android系统广播使用经验
来源:互联网 发布:软件用户手册模板 编辑:程序博客网 时间:2024/05/22 03:21
安卓配置广播方式有两种:
一:配置注册
1、mainfase.xml文件里配置
<receiver android:name=".Receive" >
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
。。。
</intent-filter>
</receiver>
2、创建Receive继承BroadcastReceiver的类
public class Receive extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
System.out.println("!!!!!action!!!!!!!" + action);
if (Intent.ACTION_SCREEN_ON.equals(action)) {
System.out.println("!!!Receive !!!ACTION_SCREEN_ON!!!!!!");
} else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
System.out.println("!!!Receive !!ACTION_SCREEN_OFF!!!!!!!");
}
}
}
二:动态注册
1、在其activity里注册广播
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(new TestReceive(), filter);
2、创建TestReceive继承BroadcastReceiver的类
public class TestReceiveextends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
System.out.println("!!!!!action!!!!!!!" + action);
if (Intent.ACTION_SCREEN_ON.equals(action)) {
System.out.println("!!!Receive !!!ACTION_SCREEN_ON!!!!!!");
} else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
System.out.println("!!!Receive !!ACTION_SCREEN_OFF!!!!!!!");
}
}
}
通过测试,发现两种注册方式有不一样的效果(暂时发现这些,以后有其他发现会继续补充):
1、执行先后:
当你同时注册了两种注册广播方式,那么当广播触发时,动态注册要先执行(前提是两种广播都要前注册)
2、是否执行问题:
上面举例中,有Intent.ACTION_SCREEN_ON(打开屏幕)、Intent.ACTION_SCREEN_OFF(关闭屏幕),经过测试发现,动态广播能接收到广播,
而配置广播接收不到,这样说明,配置广播不能拦截到所以的系统广播,而动态广播可以。
总结:使用广播时,优先考虑动态广播,但有些特殊的广播类型不得不使用配置广播,如Intent.BOOT_COMPLETED(开机广播),所以在实际应用中,
要合理的使用哪种类型广播
- android系统广播使用经验
- Android常用的系统广播的使用
- Android使用广播监听网络状态变化 一直收到系统广播 广播过滤
- Android 广播BroadcastReceiver静态/动态注册,获取系统电量广播【开机广播】,获取网络状态广播,Timer的使用简介
- Android 系统广播大全
- Android系统广播列表
- Android: 常用系统广播
- Android 系统广播
- Android 系统广播大全
- android中的系统广播
- Android常用系统广播
- Android系统广播
- Android 系统广播大全
- Android 系统广播
- Android 系统广播大全
- Android 系统广播大全
- Android 系统广播大全
- Android 系统广播大全
- thinkphp学习小结
- How to test Apache CloudStack 4.2 on your local machine
- 在JavaScript中判断整型的N种方法示例介绍
- openwrt uci 设置网络命令
- android 删除SD卡或者手机的缓存图片和目录
- android系统广播使用经验
- python的format函数
- Linux wget命令设置代理
- Live555/ffmpeg(NEON支持)/boost交叉编译(整理版)
- 为资源Resource创建别名 引用style资源
- 根据图片名字在drawable中得到图片
- Java基本开发环境搭建
- SQL存储过程前面有语句set ANSI_NULLS ON set QUOTED_IDENTIFIER ON
- windows下如何正确使用Jconsole远程连接linux主机上的JVM