LocalBroadcastManager 的使用

来源:互联网 发布:上瘾网络剧资源 编辑:程序博客网 时间:2024/05/19 18:42

对于网上的很多文章感觉介绍的并不好,LocalBroadcastManager和普通的Broadcast还是有一定区别的,下面的感觉总结的还不错,拿来记录一下

LocalBroadcastManager基本介绍 这个类是在v4包中的,谷歌官方的介绍是:

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 sendBroadcast(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.本人献丑翻译下:
 能够完成在应用内的广播发送,而且比全局广播更具优势:
 1).广播只会在你的应用内发送,所以无需担心数据泄露,更加安全。
 2).其他应用无法发广播给你的应用,所以也不用担心你的应用有别人可以利用的安全漏洞
 3).相比较全局广播,它不需要发送给整个系统,所以更加高效。

2. 使用方式
广播注册:

LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(getActivity());IntentFilter filter = new IntentFilter();filter.addAction(ACTION);myBroadcastReciver = new MyBroadcastReciver();localBroadcastManager.registerReceiver(myBroadcastReciver, filter);


广播发送

Intent intent = new Intent();intent.setAction(SaleLeftFragment.ACTION);intent.putExtra(TAG, data);LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent);


 

3.使用注意

在使用的时候,请关注以下几点:

1).LocalBroadcastManager注册广播只能通过代码注册的方式。

2).LocalBroadcastManager注册广播后,一定要记得取消监听。

3).重点的重点,使用LocalBroadcastManager注册的广播,您在发送广播的时候务必使用LocalBroadcastManager.sendBroadcast(intent);否则您接收不到广播,不要怪政府哈。

文章摘要转载自:http://www.cnblogs.com/xilinch/p/4238122.html

 

1 0
原创粉丝点击