简介
标准广播
无法截断,所有广播接收器都可收到
有序广播
- 具有优先级,广播接收器有接收的先后顺序
- 可被截断
广播的接收
动态注册监听网络变化
实例化一个IntentFilter,添加动作android.net.conn.CONNECTIVITY_CHANGE
intentFilter = new IntentFilter(); intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
定义广播接收者
class NetworkChangeReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Toast.makeText(MainActivity.this, "net change", Toast.LENGTH_SHORT).show(); } }
实例化广播接收者,并且添加注册解注册
networkChangeReceiver = new NetworkChangeReceiver(); registerReceiver(networkChangeReceiver, intentFilter); //在onCreate中注册,就在onDestory中解注册。同样onResume与onPause protected void onDestroy() { super.onDestroy(); unregisterReceiver(networkChangeReceiver); }
静态注册实现开机启动
动态注册时,只有在程序启动之后才可以接收到广播,因为注册的逻辑在onCreate中(不过粘性广播是个特例,又不过粘性广播已经在5.0废除),像开机启动这种就要静态注册
1. 新建BootCompleteReceiver
public class BootCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Boot Complete", Toast.LENGTH_SHORT).show();
}
}
2. 在清单文件中加入权限和注册BootCompleteReceiver
开机监听权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
注册广播接收器
<receiver android:name=".BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
在onReceive()中不要进行耗时操作,也不允许开启线程,一种打开程序其他组件的角色,比如创建一条状态栏通知,或者启动一个服务
发送自定义广播
发送标准广播
建立广播接收器
public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { } }
注册广播接收器
自定义了action:com.example.broadcasttest. MY_BROADCAST <receiver android:name=".MyBroadcastReceiver"> <intent-filter> <action android:name="com.example.broadcasttest. MY_BROADCAST"/> </intent-filter> </receiver>
3.发送广播
Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST"); sendBroadcast(intent);
发送有序广播
发送
Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST"); sendOrderedBroadcast(intent, null);
设置优先级
<receiver android:name=".MyBroadcastReceiver"> <intent-filter android:priority="100" > <action android:name="com.example.broadcasttest.MY_BROADCAST"/> </intent-filter> </receiver>
截断广播
public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //截断广播 abortBroadcast(); } }
使用本地广播
发出的广播只能够在应用程序的内部进行传递,并且广播接收器也只能接收来自本应用程序发出的广播
- 使用 LocalBroadcastManager替代Context,除此之外与全局广播 ,即注册、解注册、发送都是用LocalBroadcastManager
本地广播步骤:
private LocalBroadcastManager localBroadcastManager;
//实例化LocalBroadcastManager
localBroadcastManager = LocalBroadcastManager.getInstance(this);
//发送广播
Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
/**
全局广播使用Context.sendBroadcast(),而本地广播使用localBroadcastManager.sendBroadcast(intent),所以说使用LocalBroadcastManager替代Context
**/
localBroadcastManager.sendBroadcast(intent);
//注册接收器
intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");
mReceiver = new MyReceiver();
localBroadcastManager.registerReceiver(mReceiver, intentFilter);
//解注册
protected void onDestroy() {
super.onDestroy();
localBroadcastManager.unregisterReceiver(localReceiver);
}