从 Android 中的 Service 启动 Activity
Android:
public class LocationService extends Service {
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
startActivity(new Intent(this, activity.class));
}
}
我从 Activity
启动此服务
在 Activity
中,如果条件满足
startService(new Intent(WozzonActivity.this, LocationService.class));
从上面提到的 LocationService
启动,则无法启动 Activity
>,如何获取服务类中当前正在运行的Activity
的上下文?
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
In Activity
if condition satisfies start
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
从 Service 类内部:
但是,由于电池优化限制,这在
Android 10
+ 中不起作用From inside the Service class:
But, this does not work from
Android 10
+ due to battery optimisation restrictions更新 ANDROID 10 及更高版本
不再允许从服务(前台或后台)启动活动。
仍然有一些限制可以在文档中看到
https://developer.android.com/guide/components/activities/background-starts
UPDATE ANDROID 10 AND HIGHER
Start an activity from service (foreground or background) is no longer allowed.
There are still some restrictions that can be seen in the documentation
https://developer.android.com/guide/components/activities/background-starts
我遇到了同样的问题,并且想让您知道上述方法都不适合我。
对我有用的是:
在我的一个子类中,存储在一个单独的文件中,我必须:
所有其他答案都给了我一个空指针异常。
I had the same problem, and want to let you know that none of the above worked for me.
What worked for me was:
and in one my subclasses, stored in a separate file I had to:
All the other answers gave me a
nullpointerexception
.另一件值得一提的事情是:虽然当我们的任务在后台时上面的答案工作得很好,但如果我们的任务(由服务+一些活动组成)在前台(即我们的活动之一可见),我可以让它工作的唯一方法对用户来说)是这样的:
我不知道 ACTION_VIEW 或 FLAG_ACTIVITY_NEW_TASK 在这里是否有任何实际用途。成功的关键
当然是 FLAG_ACTIVITY_REORDER_TO_FRONT ,以便不再实例化活动。祝你好运!
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:
I do not know whether ACTION_VIEW or FLAG_ACTIVITY_NEW_TASK are of any actual use here. The key to succeeding was
and of course FLAG_ACTIVITY_REORDER_TO_FRONT for not instantiating the activity again. Best of luck!
Android 10 及更高版本的新方法是 SYSTEM_ALERT_WINDOW 权限。操作方法如下:
在 AndroidManizest.xml 文件中声明此权限
在单击按钮时的 onCreate 方法中,调用辅助方法来显示一个对话框,这样如果用户选择允许权限,将打开覆盖权限的设备设置。请注意,单击按钮上的这个只是为了让您了解如何调用权限。您可以根据应用程序要求以您喜欢的方式调用它。
下面声明了辅助方法吗?
一旦您相应地实现了上述代码,您就可以从服务类启动任何活动。您的活动将以编程方式启动。
基本上,您可以这样做。效果很好。顺便说一句,您可以按照自己的意愿操作此代码。希望这会有所帮助!
The new way from android 10 and greater is SYSTEM_ALERT_WINDOW permission. Here is how you do it:
Within AndroidManifiest.xml file declare this permission
Within onCreate method on button click, call a helper method to show a dialog that way if a user opted in to allow permission, device settings for overlay permission will open. Note that this on Click button is just for you to get an idea on how to invoke permission. You can invoke it which ever way you like per your app requirements.
Below are helper methods declared?
Once you implemented above codes accordingly, you can start any activity from service class. Your activity will be launched programmatically.
Basically, this is how you can do it. It works good. BTW, you can manipulate this code as you like per your desires. Hopefully, this will help!!
无法使用
Service
的Context
;能够获得类似的(包)Context
:one cannot use the
Context
of theService
; was able to get the (package)Context
alike:您还可以在Service中使用getApplicationContext()方法来运行startActivity() 方法如下:
You can also use getApplicationContext() method in your Service to run the startActivity() method as written below:
在 Service 类中,我们可以使用 this 关键字来引用上下文。
Inside the Service class, we can use the this keyword to refer to the context.
如果您需要从您的服务中重新调用 bacgrounde 中的活动,我建议您使用以下链接。 Intent.FLAG_ACTIVITY_NEW_TASK 不是解决方案。
https://stackoverflow.com/a/8759867/1127429
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.
https://stackoverflow.com/a/8759867/1127429
或者,
您可以使用自己的应用程序类并从您需要的任何地方(尤其是非活动)进行调用。
并注册您的应用程序类:
然后调用:
Alternately,
You can use your own Application class and call from wherever you needs (especially non-activities).
And register your Application class :
Then call :
SYSTEM_ALERT_WINDOW.requrid permission in manifest and enable from from mobile seting manully its works from android 10+.
SYSTEM_ALERT_WINDOW.requrid permission in manifest and enable from from mobile seting manully its works from android 10+.
小米红米4A,安卓7,MIUI 10.2.3。
debug
版本中应用的主要活动无法从服务启动到前台。logcat
中存在MIUILOG- Permission Denied Activity
错误。在release
版本中重建此应用程序修复了该问题。Xiaomi Redmi 4A, Android 7, MIUI 10.2.3. Main activity for app in
debug
version could not start to foreground from service. There wasMIUILOG- Permission Denied Activity
error inlogcat
. Rebuilding this app inrelease
version fixed the problem.