android从服务开始活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3606596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android start activity from service
提问by d-man
Android:
安卓:
public class LocationService extends Service {
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
startActivity(new Intent(this, activity.class));
}
}
I launched this service from Activity
我从 Activity
In Activity
if condition satisfies start
在Activity
if 条件满足开始
startService(new Intent(WozzonActivity.this, LocationService.class));
from my LocationService
mentioned above could not launch Activity
, how can I get context of current running Activity
in service class?
从我LocationService
上面提到的无法启动Activity
,如何获取当前Activity
在服务类中运行的上下文?
回答by d-man
From inside the Service class:
从服务类内部:
Intent dialogIntent = new Intent(this, MyActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
回答by Andy
I had the same problem, and want to let you know that none of the above worked for me. What worked for me was:
我遇到了同样的问题,想让您知道上述方法都不适合我。对我有用的是:
Intent dialogIntent = new Intent(this, myActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(dialogIntent);
and in one my subclasses, stored in a separate file I had to:
在一个我的子类中,存储在一个单独的文件中,我必须:
public static Service myService;
myService = this;
new SubService(myService);
Intent dialogIntent = new Intent(myService, myActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
myService.startActivity(dialogIntent);
All the other answers gave me a nullpointerexception
.
所有其他答案都给了我一个nullpointerexception
.
回答by kellogs
Another thing worth mentioning: while the answer above works just fine when our task is in the background, the only way I could make it work if our task (made of service + some activities) was in the foreground (i.e. one of our activities visible to user) was like this:
另一件值得一提的事情:虽然当我们的任务在后台时上面的答案工作得很好,但如果我们的任务(由服务 + 一些活动组成)在前台(即我们的一个活动可见),我可以让它工作的唯一方法用户)是这样的:
Intent intent = new Intent(storedActivity, MyActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
storedActivity.startActivity(intent);
I do not know whether ACTION_VIEW or FLAG_ACTIVITY_NEW_TASK are of any actual use here. The key to succeeding was
我不知道 ACTION_VIEW 或 FLAG_ACTIVITY_NEW_TASK 在这里是否有任何实际用途。成功的关键是
storedActivity.startActivity(intent);
and of course FLAG_ACTIVITY_REORDER_TO_FRONT for not instantiating the activity again. Best of luck!
当然还有 FLAG_ACTIVITY_REORDER_TO_FRONT 用于不再实例化活动。祝你好运!
回答by Martin Zeitler
one cannot use the Context
of the Service
; was able to get the (package) Context
alike:
一个不能使用Context
的Service
;能够获得(包)Context
类似的:
Intent intent = new Intent(getApplicationContext(), SomeActivity.class);
回答by james
Alternately,
交替,
You can use your own Application class and call from wherever you needs (especially non-activities).
您可以使用自己的 Application 类并从您需要的任何地方(尤其是非活动)调用。
public class App extends Application {
protected static Context context = null;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
public static Context getContext() {
return context;
}
}
And register your Application class :
并注册您的应用程序类:
<application android:name="yourpackage.App" ...
Then call :
然后调用:
App.getContext();
回答by GMG
If you need to recal an Activity that is in bacgrounde, from your service, I suggest the following link. Intent.FLAG_ACTIVITY_NEW_TASK is not the solution.
如果您需要从您的服务中重新调用 bacgrounde 中的 Activity,我建议使用以下链接。Intent.FLAG_ACTIVITY_NEW_TASK 不是解决方案。