BroadcastReceiver详解(二)——两种注册方式
2016-06-02 00:00
573 查看
摘要: BroadCast的注册方式有两种动态注册和静态注册
注意:动态注册的广播一定要在Activity或Service内取消注册广播,如果不取消,Activity会保持引用,无法释放资源。
动态注册的方法
Returns
The first sticky intent found that matches filter, or null if there are none.
Returns
The first sticky intent found that matches filter, or null if there are none.
动态注册取消的方法:
事例代码:
注意:android3.1以后,首次安装未启动或者用户强制force stop后,应用无法监听到
实例代码:
动态注册
特点:动态注册的广播的有效期与Activity或Service相同,当Activity或Service销毁后,动态注册的广播将不在存在。注意:动态注册的广播一定要在Activity或Service内取消注册广播,如果不取消,Activity会保持引用,无法释放资源。
动态注册的方法
public abstract Intent registerReceiver (BroadcastReceiver receiver,IntentFilter filter)
Parametersreceiver | The BroadcastReceiver to handle the broadcast. |
---|---|
filter | Selects the Intent broadcasts to be received. |
The first sticky intent found that matches filter, or null if there are none.
public abstract Intent registerReceiver (BroadcastReceiver receiver,IntentFilter filter, String broadcastPermission, Handler scheduler)
Parametersreceiver | The BroadcastReceiver to handle the broadcast. |
---|---|
filter | Selects the Intent broadcasts to be received. |
broadcastPermission | String naming a permissions that a broadcaster must hold in order to send an Intent to you. If null, no permission is required. |
scheduler | Handler identifying the thread that will receive the Intent. If null, the main thread of the process will be used. |
The first sticky intent found that matches filter, or null if there are none.
动态注册取消的方法:
public abstract void unregisterReceiver (BroadcastReceiver receiver)
Parametersreceiver | The BroadcastReceiver to unregister |
---|
//onResum中注册 sdcardStateReceiver = new SdcardStateChanageReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_MEDIA_REMOVED); filter.addAction(Intent.ACTION_MEDIA_EJECT); filter.ad 3ff0 dAction(Intent.ACTION_MEDIA_MOUNTED); filter.addDataScheme("file"); registerReceiver(sdcardStateReceiver, filter); //onStop中取消注册 unregisterReceiver(sdcardStateReceiver); }
静态注册
特点:永久存在,即使Application已经退出,注册也会有效。注意:android3.1以后,首次安装未启动或者用户强制force stop后,应用无法监听到
实例代码:
<receiver android:name=".receiver.BootCompleteReceiver" > <intent-filter android:priority="1000" > <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver>
相关文章推荐
- Android 手机 开发
- DNSPod
- Xcode Unknown type name 'NSString'
- IOS——objc_setAssociatedObject关联的使用
- Ngin的启动与停止
- 使用_allow_resetlogs_corruption打开无归档日志rman备份库
- 排序——排序的基本概念
- urlrewriter转载
- eclipse中Server视图加载项目之后项目名后边有带括号的名字
- laravel
- web测试用例管理系统介绍
- CI_数据库构造查询
- char *a 和char a[] 的区别(指针和数组的区别)
- C++ 多线程学习
- 多叉树的构建及遍历
- 判断字符是否为中文
- 拖动排序算法
- mvc设计模式和mvc框架的区别
- 分布式文件系统FastDFS设计原理
- 10个实用的但偏执的Java编程技术