1广播的分类
标准广播:完全异步的广播,一旦发出后,所有监听它的广播都会在同一时间收到该信息。
有序广播:同步执行的广播,优先级高的接收器会先收到消息,并且可被截断。
2广播的注册与接收
动态注册
public class MainActivity extends AppCompatActivity {
private IntentFilter intentFilter;
private MyReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentFilter = new IntentFilter();
receiver = new MyReceiver();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(receiver, intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
//动态注册的广播要在onDestroy()方法中取消注册
unregisterReceiver(receiver);
}
}
广播接收器类
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "network changes", Toast.LENGTH_SHORT).show();
}
}
静态注册
在<receiver>标签下通过<intent-filter>标签添加<action>标签来实现
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<!--开机监听-->
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
注意相应权限的添加
发送自定义广播
标准广播
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.shaw.myapplication.MyBroadcastReceiver"/>
</intent-filter>
</receiver>
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new
Intent("com.example.shaw.myapplication.MyBroadcastReceiver");
sendBroadcast(intent);
}
});
有序广播
<receiver
android:name=".OrderBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100"> <!-- 设置接收器的优先级 -->
<action android:name="com.example.shaw.myapplication.OrderBroadcastReceiver" />
</intent-filter>
</receiver>
<receiver
android:name=".OrderBroadcastReceiver2"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.shaw.myapplication.OrderBroadcastReceiver" />
</intent-filter>
</receiver>
有序广播的发送
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new
Intent("com.example.shaw.myapplication.OrderBroadcastReceiver");
//第二个参数为与权限相关的字符串,null表示所有接收器都可以接收
sendOrderedBroadcast(intent,null);
}
});
广播接收器1
public class OrderBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("OrderBroadcastReceiver", "OrderBroadcastReceiver in received");
//截断广播
abortBroadcast();
}
}
广播接收器2
public class OrderBroadcastReceiver2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("OrderBroadcastReceiver", "OrderBroadcastReceiver2 in received");
}
}
运行结果为
从Log打印日志可看出广播接收器2没有接受到广播,也就说明了广播被接收器1截断了
本地广播
产生原因:系统全局广播因为可能被其他数据截断造成数据泄露,其他程序也可能向我们的广播接收器发送垃圾广播。
本地广播只能在程序内部进行传递。
localBroadcastManager = LocalBroadcastManager.getInstance(this);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.example.shaw.myapplication.LOCAL_BROADCAST");
//使用LocalBroadcastManager实例发送本地广播
localBroadcastManager.sendBroadcast(intent);
}
});
intentFilter = new IntentFilter();
intentFilter.addAction("com.example.shaw.myapplication.LOCAL_BROADCAST");
//注册本地广播监听器
localBroadcastManager.registerReceiver(localReceiver, intentFilter);
registerReceiver(localReceiver2, intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
//使用LocalBroadcastManager实例取消注册
localBroadcastManager.unregisterReceiver(localReceiver);
unregisterReceiver(localReceiver2);
}
public class LocalReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("LocalReceiver", "LocalReceiver received");
}
}
public class LocalReceiver2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("LocalReceiver", "LocalReceiver2 received");
}
}
该例子中,localReceiver采用的本地广播注册,而localReceiver2采用的是标准广播注册,从Log日志打印中可看出localReceiver2确实没有接收到该广播。
注意:本地广播只能采用动态注册的方式