导语: 在Android系统中,根据广播的执行顺序不同,可将其分为有序广播和无序广播。有序广播是一种同步执行的广播,在广播发出之后,同一时刻只会有一个广播接收器能够接收到这条消息,当这个广播接收器中的逻辑执行完毕后,广播才会继续传递。所以,此时的广播接收器是有先后顺序的,并且可以被拦截。有序广播发送一条消息后,高优先级的广播接收器先接收到广播,低优先级的广播后接收到广播。想要拦截一条广播,可使用abortBroadcast();方法。
下面具体介绍如何实现拦截有序广播
1.用户交互界面的设计与实现
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/stitch_one"
tools:context="com.example.bz0209.orderedbroadcast.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:onClick="send"
android:text="发送有序广播"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:background="#FBFBFF"
android:textSize="20sp"/>
</RelativeLayout>
2.界面交互代码的设计与实现
package com.example.bz0209.orderedbroadcast;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void send(View view){
Intent intent=new Intent();
intent.setAction("Intercept_Switch");
sendOrderedBroadcast(intent,null);
}
}
3.创建三个广播接收者:
<1>MyBroadcastReceiverOne.java
package com.example.bz0209.orderedbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverOne extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyBroadcastReceiverOne","自定义的广播接收者One,接收到了广播事件");
}
}
<2>MyBroadcastReceiverTwo.java
package com.example.bz0209.orderedbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverTwo extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyBroadcastReceiverTwo","自定义的广播接收者Two,接收到了广播事件");
abortBroadcast();//拦截有序广播
Log.i("MyBroadcastReceiverTwo","我是接收者Two,广播被我终结了");
}
}
<3>MyBroadcastReceiverThree.java
package com.example.bz0209.orderedbroadcast;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverThree extends BroadcastReceiver {
@SuppressLint("LongLogTag")
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyBroadcastReceiverThree","自定义的广播接收者Three,接收到了广播事件");
}
}
4.设置优先级广播接收者的优先级
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bz0209.orderedbroadcast">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<receiver android:name=".MyBroadcastReceiverOne">
<intenf-filter android:priority="1000">
<action android:name="Intercept_Stitch"/>
</intenf-filter>
</receiver>
<receiver android:name=".MyBroadcastReceiverTwo">
<intenf-filter android:priority="200">
<action android:name="Intercept_Stitch"/>
</intenf-filter>
</receiver>
<receiver android:name=".MyBroadcastReceiverThree">
<intenf-filter android:priority="600">
<action android:name="Intercept_Stitch"/>
</intenf-filter>
</receiver>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100"/>
</receiver>
</activity>
</application>
</manifest>
其中在清单文件中注册广播接收者<intent-filter android:priority="100"/>属性指定数值越大,优先级越高。如果两个广播接收者的优先级相同,则先注册的组件优先接收到广播。如果两个应用程序监听了同一个广播事件并设置了优先级,则先安装的应用优先接收到广播。
5.运行效果如下