LocalBroadcastManager VS Context.registerReceiver(),Context.sendBroadcast(意向),和Context.unregisterReceiver()他们是一样的吗? [英] LocalBroadcastManager vs Context.registerReceiver(), Context.sendBroadcast(Intent), and Context.unregisterReceiver() are they same?

查看:500
本文介绍了LocalBroadcastManager VS Context.registerReceiver(),Context.sendBroadcast(意向),和Context.unregisterReceiver()他们是一样的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 Context.registerReceiver(),Context.sendBroadcast(意向),和Context.unregisterReceiver()

但是当我看到这个类 LocalBroadcastManager ,有 registerReceiver(),sendBroadcast(意向), unregisterReceiver()上下文

but when I saw the class LocalBroadcastManager, it has registerReceiver(), sendBroadcast(Intent) ,andunregisterReceiver() like in Context.

我很困惑。什么时候应该使用 LocalBroadcastManager ?他们是同在上下文

I'm confused. When should I use the LocalBroadcastManager? are they same in Context?

问候,

谢谢...

推荐答案

LocalBroadcastManager是因为它的名字一样,的广播方式是的实现提供给你的应用程序。这有一定的好处,用最大的幸福安全,少了一个需要注意的洞。在实施方面,有几件事情要记住:

LocalBroadcastManager is as its name says, an implementation of the broadcast methods that are only available to your app. This has some benefits, with the biggest being safety, one less hole to watch out for. In terms of implementation, there are a few things to keep in mind:

  • 在这个类是从Android支持库
  • 的方法调用必须是prefaced与 LocalBroadcastManager.getInstance([背景]),其中 [背景] 是一个子类的Context类,如活动。
  • 当你使用这个类,它是纯粹为应用。使用它来注册的接收器,使广播是整个系统的失败。
  • This class is from the Android Support Library
  • The method calls have to be prefaced with LocalBroadcastManager.getInstance([CONTEXT]) where [CONTEXT] is a subclass of the Context class, such as Activity.
  • When you use this class, it is purely for your app. Using it to register receivers and make broadcasts that are system wide will fail.

所以这个类是不一样的背景下,它只是一个非常具体的,应用程序,只实现Context的接收器/广播方式。你应该使用它时,也绝对没有点你的听众对全局(系统级)广播听,当你的广播不需要你的应用程序之外的任何目标。

So this class is not the same as Context, it is simply a very specific, app-only implementation of Context's receiver/broadcast methods. You should use it when there is absolutely no point for your listener to listen on global (system-wide) broadcasts and when your broadcast does not need to target anything outside your app.

这篇关于LocalBroadcastManager VS Context.registerReceiver(),Context.sendBroadcast(意向),和Context.unregisterReceiver()他们是一样的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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