Android中的静态系统广播和动态系统广播

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//网络状态变化

<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>


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 //时间变化  每分钟一次

<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);



  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值