- 以下是谷歌api官方文档
- The
LocalBroadcastManager.sendBroadcast
method sends broadcasts to receivers that are in the same app as the sender. If you don't need to send broadcasts across apps, use local broadcasts. The implementation is much more efficient (no interprocess communication needed) and you don't need to worry about any security issues related to other apps being able to receive or send your broadcasts. - 由此得知使用LocalBroadCast有如下好处
1 因广播数据在本应用范围内传播,你不用担心隐私数据泄露的问题。
2 不用担心别的应用伪造广播,造成安全隐患。
3 相比在系统内发送全局广播,它更高效。
其使用方法也和正常注册广播类似:
具体demo
这里使用LocalBroadcastManager这个单例类来进行实现,具体操作代码如下:
定义一个action:
然后封装发送广播:
广播接收者:
注册与取消