如何从非活动类启动活动? [英] How can I start an Activity from a non-Activity class?
问题描述
我有一个地图视图活动,其中显示了一些 OverlayItems
.例如,在叠加层的 onTap
方法中,我想触发一个新活动,以全屏显示此叠加层照片.
I have a map view activity that shows a few OverlayItems
. In the onTap
method of an overlay, I want to trigger a new activity that shows this overlay photo as a fullscreen, for example.
当我在叠加类中执行此操作时:
When I do this inside my overlay class:
Intent intent = new Intent();
intent.setClass(getApplicationContext, FullscreenView.class);
startActivity(intent);
...它找不到应用程序上下文,因为我不在活动范围内.
.. it can't find an application context, as I am not in the scope of an activity.
当我向我的主要活动添加一个方法时,假设 startFullscreen
:
When I add a method to my main activity, let's say startFullscreen
:
public static void startFullscreen() {
if (sCurrentPhoto != null) {
Intent intent = new Intent();
intent.setClass(getApplicationContext(), FullscreenView.class);
startActivity(intent);
}
}
我无法调用 getApplicationContext()
和 startActivity()
,因为我处于静态上下文中.但是,我需要静态方法在 Overlay 类中调用它,例如 MainView.startFullscreen()
.
I can not call getApplicationContext()
and startActivity()
, because I am in a static context. I need the static method however to call it in the Overlay class like MainView.startFullscreen()
.
简单地说:如何从非 Activity 类启动 Activity?
推荐答案
您的 onTap
覆盖接收 MapView
,您可以从中获取 Context
>:
Your onTap
override receives the MapView
from which you can obtain the Context
:
@Override
public boolean onTap(GeoPoint p, MapView mapView)
{
// ...
Intent intent = new Intent();
intent.setClass(mapView.getContext(), FullscreenView.class);
startActivity(intent);
// ...
}
这篇关于如何从非活动类启动活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!