BroadcastReceiver的或Messenger通过处理程序 [英] BroadcastReceiver or Messenger via Handler

查看:131
本文介绍了BroadcastReceiver的或Messenger通过处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 IntentService 这需要传递一个消息给活动。 我知道这样做的两种方式。

I have an IntentService which need to pass a message to an Activity. I know two ways of doing so.

  1. 使用 sendBroadcast()服务一边登记 broadcastReciever 活动侧,将接收的消息。

  1. use sendBroadcast() at the Service side while registering a broadcastReciever at the Activity side which will receiver the message.

传递使者到服务,它会指向一个处理程序活动端,这将是准备接收消息的服务。

passing a Messenger to the Service side, which will point to a Handler at the Activity side, which will be ready to receive that message from the service.

哪一个是它的目的是好的?或者两者做?

Which one is good for which purpose? Or both of them do the same?

推荐答案

如果你的 IntentService 不知道活动是否会存在(例如,可能已被破坏),或者是否有可能是在前台,并会想知道发生了什么事情,我会使用多种活动 sendOrderedBroadcast()。你可以安排到那么也有一个逆止器的BroadcastReceiver ,可以提出一个通知,如果需要的话,我勾勒出的这个博客帖子和演示的此示例项目

If your IntentService does not know whether the activity will exist (e.g., might have been destroyed), or if there are multiple activities that might be in the foreground and would want to know about what's going on, I'd use sendOrderedBroadcast(). You can arrange to then also have a "backstop" BroadcastReceiver that could raise a Notification, if desired, as I outline in this blog post and demonstrate in this sample project.

无论你的技术可以工作,虽然。

Either of your techniques can work, though.

这篇关于BroadcastReceiver的或Messenger通过处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆