本文介绍了安卓:我怎样才能获得当前的前台活动(从服务)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种原生的Android方式来获得一个服务的引用,当前运行的活动?
我在后台运行的服务,我想,当一个事件发生时(在服务)来更新我的当前活动。有没有一种简单的方法来做到这一点(如我上述建议)?
解决方案
您可能没有自己的当前正在运行的活动。
- 发送广播
意图
的活动 - 的证明这个模式李示例项目> - 有活动提供一个
PendingIntent
(例如,通过createPendingResult()
),该服务调用李> - 有活动通过
注册服务的回调或侦听器对象bindService()
,并有服务调用的回调/监听器对象上的事件方法李> - 发送有序广播
意图
的活动,具有低优先级的的BroadcastReceiver
备份(提高一通知
如果该活动没有在屏幕上) - 的有更多关于这个模式
Is there a native android way to get a reference to the currently running Activity from a service?
I have a service running on the background, and I would like to update my current Activity when an event occurs (in the service). Is there a easy way to do that (like the one I suggested above)?
解决方案
You may not own the "currently running Activity".
- Send a broadcast
Intent
to the activity -- here is a sample project demonstrating this pattern - Have the activity supply a
PendingIntent
(e.g., viacreatePendingResult()
) that the service invokes - Have the activity register a callback or listener object with the service via
bindService()
, and have the service call an event method on that callback/listener object - Send an ordered broadcast
Intent
to the activity, with a low-priorityBroadcastReceiver
as backup (to raise aNotification
if the activity is not on-screen) -- here is a blog post with more on this pattern
这篇关于安卓:我怎样才能获得当前的前台活动(从服务)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!