2.1全局广播
2.1.1广播类型
-
标准广播
是一种异步广播,在广播发出后,所有的广播接收器几乎在同一时刻接收到这条消息。因此,标准广播没有任何的先后顺序可言。相对来说,这种广播的接收效率会高一些。
-
有序广播
是一种同步广播,在广播发出后,同一时刻只有一个广播接收器可以接收这条消息,当这个接收器处理完逻辑任务后,广播才会继续向下传递。因此,有序广播的的接收器有先后顺序,优先级高的广播接收器就会优先接收到消息,并且有截断广播的权力。
2.1.2广播注册方式
-
动态注册
广播接收器可以自由的控制注册和注销,在灵活方面有着很大的优势,但是有一个很大的缺点是必须在执行了注册代码后发送广播,这个接收器才会接收到!
-
静态注册
静态注册相对于动态注册,灵活性降低了许多,因为是在AndroidManifest文件中进行注册,是一个无法注销的广播。
2.1.3发送标准广播
- 准备广播接收器(为了测试,准备两个接收器)
private class MBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//逻辑代码
Toast.makeText(MainActivity.this, "动态注册", Toast.LENGTH_LONG).show();
}
}
public class StaticReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//逻辑代码
Toast.makeText(context, "静态注册", Toast.LENGTH_SHORT).show();
}
}
- 注册广播(两个接收器两种注册方式)
动态注册
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMBroadcastReceiver = new MBroadcastReceiver();
IntentFilter mIntentFilter = new IntentFilter();
mIntentFilter.addAction("com.nightrain.broadcastreceiverdemo.standard");
registerReceiver(mMBroadcastReceiver, mIntentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mMBroadcastReceiver != null) {
//动态注册记得注销
unregisterReceiver(mMBroadcastReceiver);
}
}
静态注册
在AndroidManifest文件中注册
<receiver
android:name=".StaticReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.nightrain.broadcastreceiverdemo.standard">
</intent-filter>
</receiver>
- 发送标准广播
Intent mIntent = new Intent();
mIntent.setAction("com.nightrain.broadcastreceiverdemo.standard");
mIntent.setPackage(getPackageName());
sendBroadcast(mIntent);
2.1.4发送有序广播
- 准备广播接收器(同样两个接收器,这次在静态接收器中设置一个拦截器)
private class MBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//逻辑代码
Toast.makeText(MainActivity.this, "动态注册", Toast.LENGTH_LONG).show();
}
}
public class StaticReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//逻辑代码
Toast.makeText(context, "静态注册", Toast.LENGTH_SHORT).show();
//拦截广播
abortBroadcast();
}
}
- 注册广播(两个接收器两种注册方式)
动态注册
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMBroadcastReceiver = new MBroadcastReceiver();
IntentFilter mIntentFilter = new IntentFilter();
mIntentFilter.addAction("com.nightrain.broadcastreceiverdemo.standard");
registerReceiver(mMBroadcastReceiver, mIntentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mMBroadcastReceiver != null) {
//动态注册记得注销
unregisterReceiver(mMBroadcastReceiver);
}
}
静态注册
在AndroidManifest文件中注册(这次在静态广播中添加优先级)
<receiver
android:name=".StaticReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100">
<action android:name="com.nightrain.broadcastreceiverdemo.standard">
</intent-filter>
</receiver>
- 发送有序广播
Intent mIntent = new Intent();
mIntent.setAction("com.nightrain.broadcastreceiverdemo.standard");
mIntent.setPackage(getPackageName());
sendOrderedBroadcast(mIntent, null);
2.2本地广播
- 创建接收器
private class MLocalBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//逻辑代码
Toast.makeText(MainActivity.this, "本地广播", Toast.LENGTH_LONG).show();
}
}
- 注册广播
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取本地广播管理器
mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
mMLocalBroadcastReceiver = new MLocalBroadcastReceiver();
IntentFilter mLocalIntentFilter = new IntentFilter();
mLocalIntentFilter.addAction("com.nightrain.broadcastreceiverdemo.local");
//注册广播
mLocalBroadcastManager.registerReceiver(mMLocalBroadcastReceiver, mLocalIntentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mLocalBroadcastManager != null && mMLocalBroadcastReceiver != null) {
//注销广播
mLocalBroadcastManager.unregisterReceiver(mMLocalBroadcastReceiver);
}
}
- 发送广播
Intent mIntent = new Intent();
mIntent.setAction("com.nightrain.broadcastreceiverdemo.local");
mLocalBroadcastManager.sendBroadcast(mIntent);
2.3结语
-
全局广播
1、多种发送方式,使用灵活
2、可以跨程序接收消息(也就意味着数据不安全)
3、由于系统限制,API 26以上的静态广播需要指定包名才可接收到消息
-
本地广播
1、应用内部使用的广播,只有应用内部才可以接收消息,不必担心数据的安全性
2、本地广播比全局广播的效率更高
网友评论