Android中LocalBroadcastManager的使用
2015-07-17 11:23
591 查看
先聊聊LocalBroadcastManager有啥用:
1.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).广播只会在你的应用内发送,所以无需担心数据泄露,更加安全。
2).其他应用无法发广播给你的应用,所以也不用担心你的应用有别人可以利用的安全漏洞
3).相比较全局广播,它不需要发送给整个系统,所以更加高效。
使用的方式
广播注册:
广播发送
3.使用注意
在使用的时候,请关注以下几点:
1).LocalBroadcastManager注册广播只能通过代码注册的方式。
2).LocalBroadcastManager注册广播后,一定要记得取消监听。
3).重点的重点,使用LocalBroadcastManager注册的广播,您在发送广播的时候务必使用LocalBroadcastManager.sendBroadcast(intent);否则您接收不到广播,不要怪政府哈。
原文出自:http://www.cnblogs.com/xilinch/p/4238122.html
1.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).广播只会在你的应用内发送,所以无需担心数据泄露,更加安全。
2).其他应用无法发广播给你的应用,所以也不用担心你的应用有别人可以利用的安全漏洞
3).相比较全局广播,它不需要发送给整个系统,所以更加高效。
使用的方式
广播注册:
1 LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(getActivity()); 2 IntentFilter filter = new IntentFilter(); 3 filter.addAction(ACTION); 4 myBroadcastReciver = new MyBroadcastReciver(); 5 localBroadcastManager.registerReceiver(myBroadcastReciver, filter);
广播发送
1 Intent intent = new Intent(); 2 intent.setAction(SaleLeftFragment.ACTION); 3 intent.putExtra(TAG, data); 4 LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent);
3.使用注意
在使用的时候,请关注以下几点:
1).LocalBroadcastManager注册广播只能通过代码注册的方式。
2).LocalBroadcastManager注册广播后,一定要记得取消监听。
3).重点的重点,使用LocalBroadcastManager注册的广播,您在发送广播的时候务必使用LocalBroadcastManager.sendBroadcast(intent);否则您接收不到广播,不要怪政府哈。
原文出自:http://www.cnblogs.com/xilinch/p/4238122.html
相关文章推荐
- Android studio各种问题
- android如何更新
- Android开发之Is Library篇
- WeakReference 在android中的应用
- Android动画之正弦曲线运动
- Android中Listview(四)--分组listview
- 智能储物柜/快递柜系统解析<二>
- Android笔记:inflate的三个参数及其用法
- 图片显示
- Android 使用ViewPager实现左右循环滑动图片
- 【转】Android安全研究经验谈
- Android binder学习一:主要概念
- Android项目无法和服务器建立连接
- Android之断点续传(一)
- Android binder学习一:主要概念
- Android使用Java Web服务器作为中转实现即时通信
- 如何在Android模拟器上安装apk文件[转]
- Android之-----Frame帧动画(Animation)
- 对android.jar进行反编译获得其资源文件
- Android入门常见问题