我怎样才能获得当前的前台活动

我怎样才能获得当前的前台活动

本文介绍了安卓:我怎样才能获得当前的前台活动(从服务)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种原生的Andr​​oid方式来获得一个服务的引用,当前运行的活动?

我在后台运行的服务,我想,当一个事件发生时(在服务)来更新我的当前活动。有没有一种简单的方法来做到这一点(如我上述建议)?

解决方案

您可能没有自己的当前正在运行的活动。

  1. 发送广播意图的活动 - 的证明这个模式
  2. 有活动提供一个 PendingIntent (例如,通过 createPendingResult()),该服务调用
  3. 有活动通过注册服务的回调或侦听器对象bindService(),并有服务调用的回调/监听器对象上的事件方法
  4. 发送有序广播意图的活动,具有低优先级的的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".

  1. Send a broadcast Intent to the activity -- here is a sample project demonstrating this pattern
  2. Have the activity supply a PendingIntent (e.g., via createPendingResult()) that the service invokes
  3. 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
  4. Send an ordered broadcast Intent to the activity, with a low-priority BroadcastReceiver as backup (to raise a Notification if the activity is not on-screen) -- here is a blog post with more on this pattern

这篇关于安卓:我怎样才能获得当前的前台活动(从服务)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:46