Android中的静态系统广播和动态系统广播
2016-03-01 14:14
405 查看
Android4.4:
静态广播:
可在AndroidManifest.xml中定义,不需程序启动即可接收,可用作自动启动程序
Intent.ACTION_BOOT_COMPLETED //系统启动完成
Intent.ACTION_MEDIA_MOUNTED //SD卡挂载
Intent.ACTION_MEDIA_UNMOUNTED //SD卡卸载
Intent.ACTION_USER_PRESENT//解除锁屏
ConnectivityManager.CONNECTIVITY_ACTION//网络状态变化
[html] view
plain copy
<receiver android:name=".StaticBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />
</intent-filter>
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
[java] view
plain copy
public class StaticBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Log.d(TAG, "onReceive boot: ");
Intent new_intent = new Intent(context,TestLauncher.class);
//popup the activity
new_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(new_intent);
}else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Log.d(TAG, "onReceive ACTION_USER_PRESENT: ");
}
}
}
动态广播:
只能在代码中注册,程序适应系统变化做操作,程序运行状态才能接收到
Intent.ACTION_SCREEN_ON //屏幕亮
Intent.ACTION_SCREEN_OFF //屏幕灭
Intent.ACTION_TIME_TICK //时间变化 每分钟一次
[java] view
plain copy
<span style="white-space:pre"> </span> IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_TIME_TICK);
registerReceiver(new DynamicBroadcastReceiver(), filter);
静态广播:
可在AndroidManifest.xml中定义,不需程序启动即可接收,可用作自动启动程序
Intent.ACTION_BOOT_COMPLETED //系统启动完成
Intent.ACTION_MEDIA_MOUNTED //SD卡挂载
Intent.ACTION_MEDIA_UNMOUNTED //SD卡卸载
Intent.ACTION_USER_PRESENT//解除锁屏
ConnectivityManager.CONNECTIVITY_ACTION//网络状态变化
[html] view
plain copy
<receiver android:name=".StaticBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />
</intent-filter>
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
[java] view
plain copy
public class StaticBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Log.d(TAG, "onReceive boot: ");
Intent new_intent = new Intent(context,TestLauncher.class);
//popup the activity
new_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(new_intent);
}else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Log.d(TAG, "onReceive ACTION_USER_PRESENT: ");
}
}
}
动态广播:
只能在代码中注册,程序适应系统变化做操作,程序运行状态才能接收到
Intent.ACTION_SCREEN_ON //屏幕亮
Intent.ACTION_SCREEN_OFF //屏幕灭
Intent.ACTION_TIME_TICK //时间变化 每分钟一次
[java] view
plain copy
<span style="white-space:pre"> </span> IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_TIME_TICK);
registerReceiver(new DynamicBroadcastReceiver(), filter);
相关文章推荐
- android开发 wifi开发不稳定性测试
- 60条Android日常开发总结的技术经验
- Android提供的支持不同屏幕大小的全部方法
- android之JNI开发步骤总结
- android中使用jni对字符串加解密实现分析
- Android 系统应用 注意的那些事
- android中View.measure方法详解
- Android-笔记-160301
- Android中的Looper类
- 最新版Android Studio(v1.4)如何导入第三方so
- %1$s %1$d Android string
- Android shape使用教程
- Android自定义属性时TypedArray的使用方法
- Android手机系统adb常用的命令
- Android支持的媒体格式
- Android EGL, OpenGL ES
- Android下音频进行转码
- 【Android开发经验】LayoutInflater—— 你可能对它并不了解甚至错误使用
- Android的布局技巧 #3:通过合并优化
- Android studio 设置每次启动在欢迎页(选择项目的界面)