Android 广播机制简介
广播主要分为两种类型,标准广播,有序广播
项目完整代码
https://github.com/Phil-tom/Android-The-First-Line-of-Code/tree/main/Chapter5
在发送广播之前,需要定义一个广播接收器来准备接收此广播才行。
新建MyBroadcastReceiver
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive: receive in MyBroadcastReceiver");
Toast.makeText(context,"receive in MyBroadcastReceiver",Toast.LENGTH_SHORT).show();
}
}
在AndroidManifest.xml文件中对这个广播接收器进行修改,这里让MyBroadcastReceiver接收一条值为com.zzz.mybroadcastreceiver.MY_BROADCAST的广播,因此待会在MainActivity中发送广播的时候,就需要发出一条这样的广播。
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.zzz.mybroadcastreceiver.MY_BROADCAST"/>
intent-filter>
receiver>
修改activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send Broadcast"/>
LinearLayout>
修改MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: ");
Intent intent = new Intent("com.zzz.mybroadcastreceiver.MY_BROADCAST");
intent.setComponent(new ComponentName("com.zzz.mybroadcastreceiver","com.zzz.mybroadcastreceiver.MyBroadcastReceiver"));
sendBroadcast(intent);
}
});
}
}
点击Button按钮就会出现消息弹框。
需要注意的是:
在《第一行代码Andriod(第二版)》中没有下面这一句代码,我查阅资料以后发现,是android版本太高的原因,我的虚拟设备是Android 9,加上这句代码以后就可以正常的接收广播了
可以通过以下两个博客学习
Android接收不到广播可能存在的问题
Android8 自定义广播接收不到的问题
intent.setComponent(new ComponentName("com.zzz.mybroadcastreceiver","com.zzz.mybroadcastreceiver.MyBroadcastReceiver"));
广播是一种可以跨进程的通信方式,因此,我们在应用程序内发出广播,其他的应用程序应该也是可以收到的。
新建一个MyBroadcastReceiver2,创建好以后在项目下定义一个广播接收器,用于接收MyBroadcastReceiver中的自定义广播
public class AnotherBroadcastReceive extends BroadcastReceiver {
private static final String TAG = "AnotherBroadcastReceive";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive: received in AnotherBoradcasrReceiver");
Toast.makeText(context,"received in AnotherBoradcasrReceiver",Toast.LENGTH_SHORT).show();
}
}
修改AndroidManifest.xml对这个广播接收器进行修改
<receiver
android:name=".AnotherBroadcastReceive"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.zzz.mybroadcastreceiver.MY_BROADCAST"/>
intent-filter>
receiver>
AnotherBroadcastReceive同样接收的是com.zzz.mybroadcastreceiver.MY_BROADCAST,现在运行MyBroadcastRecever2将该程序装在模拟器上,然后重新回到MyBroadcastRecever的主界面,点击按钮,就会分别弹出两次提示信息
我这里是拿Android 7.0 API 24 的虚拟设备进行截图的,所以没有问题,如果是高版本的,则只会显示下面的第一张图,MyBroadcastRecever2这个应用接收不到广播。
如果使用高版本,想要MyBroadcastRecever2接收到这个广播,则可以在MainActivity中按照以下代码的写法,不过这样MyBroadcastRecever就接收不到了。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: ");
Intent intent = new Intent("com.zzz.mybroadcastreceiver.MY_BROADCAST");
// intent.setComponent(new ComponentName("com.zzz.mybroadcastreceiver","com.zzz.mybroadcastreceiver.MyBroadcastReceiver"));
intent.setPackage("com.zzz.mybroadcastreceiver2");
sendBroadcast(intent);
}
});
}
将sendBroadcast()换成sendOrderedBroadcast()方法接受两个参数,第一个参数仍然是Intent,第二个参数是一个与权限相关的字符串,这里传入null就行。
有序广播,这个时候广播接收器是有先后顺序的,而且前面的广播接收器还可以将后面的广播接收器截断,以阻止其继续传播。
设置广播接收器的前后顺序,使用android:priority属性给广播接收器设置属性,优先级比较高的广播接收器可以先收到广播。既然已经设置了优先级,就可以设置是否允许广播继续传递了。如果在onReceive()方法中调用了abortBroadcast()方法,就表示将这条广播截断,后面的广播接收器将无法再接收到这条广播。
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100">
<action android:name="com.zzz.mybroadcastreceiver.MY_BROADCAST"/>
intent-filter>
receiver>
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive: receive in MyBroadcastReceiver");
Toast.makeText(context,"receive in MyBroadcastReceiver",Toast.LENGTH_SHORT).show();
abortBroadcast();
}
}