8.Android四大组件之BroadCastReceiver

1.广播有哪些类型

  • 开机广播

  • 网络状态广播

  • 关机广播

2.注册广播方式

2.1 静态注册

在AndroidMainfest中注册

        <receiver android:name="com.example.reboottest.BootCompletedReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

当收到对应的开机广播后,则在BroadcastReceiver中进行相应的接收


if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
			
			if (PreferencesUtil.getBoolean(context, "AUTO_REBOOT", false)) {
				Intent acIntent = new Intent(context, MainActivity.class);   
				acIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
				context.startActivity(acIntent);
			}
}

2.2 动态注册

发布了100 篇原创文章 · 获赞 42 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/Hh20161314/article/details/104269923