- 官方文档解释:Helper to register for and send broadcasts of Intents to local objects within your process. This is has a number of advantages over sending global broadcasts with
Context.sendBroadcast(android.content.Intent)
:- You know that the data you are broadcasting won‘t leave your app, so don‘t need to worry about leaking private data.
- It is not possible for other applications to send these broadcasts to your app, so you don‘t need to worry about having security holes they can exploit.
- It is more efficient than sending a global broadcast through the system.
意义是:翻译下,1、你正在发送的数据不会超出你的应用,所以不用担心泄露应用私有数据。
- 2、其他应用发送的广播不会发送到你的APP,所以你不用担心有安全性的攻击。
- 3、本地广播比全局广播更高效
使用方法:非常简单,该类是采用单例,
创建
final LocalBroadcastManager localBroadcastManager=LocalBroadcastManager.getInstance(this);
注册:
IntentFilter filter=new IntentFilter(); filter.addAction("duanqing.test.localbroadcast.fly"); localBroadcastManager.registerReceiver(new MyBroadCastReceiver(),filter);
发送代码:
Intent intent=new Intent(); intent.setAction("duanqing.test.localbroadcast.fly"); localBroadcastManager.sendBroadcast(intent);
时间: 2024-11-05 20:47:51