通过处理程序的广播接收器或信使

发布于 2024-12-03 22:29:58 字数 396 浏览 3 评论 0原文

我有一个 IntentService ,它需要将消息传递给 Activity。 我知道有两种方法可以做到这一点。

  1. Service端使用sendBroadcast(),同时在Activity端注册一个broadcastReciever来接收消息。

  2. Messenger传递到Service,它将指向Activity端的Handler,它将准备好从服务接收该消息。

哪一个适合什么目的?或者两者都做同样的事情?

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

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

  2. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热情消退 2024-12-10 22:29:58

如果您的 IntentService 不知道该 Activity 是否存在(例如,可能已被销毁),或者是否有多个 Activity 可能位于前台并且想要了解正在发生的情况,我会使用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文