1.创建程序
程序对应的布局文件(activity_main.xml)如下所示:
<?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="cn.edu.bzu.orderedbroadcast.MainActivity">
<Button
android:text="发送有序广播"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:onClick="send"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:background="#FBFBFF"
android:textSize="20sp"
android:id="@+id/button" />
</RelativeLayout>
2.添加广播接受者
添加三个广播接收者MyBroadcastReceiverOne,MyBroadcastReceiverTwo,MyBroadcastReceiverThree,不同的广播接收者打印不同的提示信息。
MyBroadcastReceiverOne的代码如下:
package cn.edu.bzu.orderedbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* Created by Administrator on 2017/6/2.
*/
public class MyBroadcastReceiverOne extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyBroadcastReceiverOne","自定义的广播接收者One,接受到了广播事件");
}
}
MyBroadcastReceiverTwo的代码如下:
package cn.edu.bzu.orderedbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* Created by Administrator on 2017/6/2.
*/
public class MyBroadcastReceiverTwo extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyBroadcastReceiverTwo","自定义的广播接收者Two,接受到了广播事件");
}
}
MyBroadcastReceiverThree的代码如下:(Log.i()有时会提示出错,原因是最大支持23个字符,当方法内写成MyBroadcastReceiverThree时超出了它的容量,可以减少一个字符)
package cn.edu.bzu.orderedbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* Created by Administrator on 2017/6/2.
*/
public class MyBroadcastReceiverThree extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyBroadcastReceiverThre","自定义的广播接收者Three,接受到了广播事件");
}
}
3.在清单文件中注册三个广播接收者并为它们指定不同的优先级,具体代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.edu.bzu.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>
</activity>
<receiver android:name=".MyBroadcastReceiverOne">
<intent-filter android:priority="1000">
<action android:name="Intercept_Stitch"/>
</intent-filter>
</receiver>
<receiver android:name=".MyBroadcastReceiverTwo">
<intent-filter android:priority="200">
<action android:name="Intercept_Stitch"/>
</intent-filter>
</receiver>
<receiver android:name=".MyBroadcastReceiverThree">
<intent-filter android:priority="600">
<action android:name="Intercept_Stitch"/>
</intent-filter>
</receiver>
</application>
</manifest>
4.编写界面交互代码:
package cn.edu.bzu.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_Stitch");
sendOrderedBroadcast(intent,null);
}
}
5.运行程序拦截有序广播
观察窗口的提示信息:
优先级最高的MyBroadcastReceiverOne最先收到广播事件,其次是MyBroadcastReceiverThree,最后是MyBroadcastReceiverTwo,说明广播接收者的优先级决定广播接收的顺序。
将广播接收者MyBroadcastReceiverTwo的优先级也设置为1000,并将其的注册码放在MyBroadcastReceiverOne的前面,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.edu.bzu.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>
</activity>
<receiver android:name=".MyBroadcastReceiverTwo">
<intent-filter android:priority="1000">
<action android:name="Intercept_Stitch"/>
</intent-filter>
</receiver>
<receiver android:name=".MyBroadcastReceiverOne">
<intent-filter android:priority="1000">
<action android:name="Intercept_Stitch"/>
</intent-filter>
</receiver>
<receiver android:name=".MyBroadcastReceiverThree">
<intent-filter android:priority="600">
<action android:name="Intercept_Stitch"/>
</intent-filter>
</receiver>
</application>
</manifest>
运行结果如图:
高优先级的广播是可以被终止的,在MyBroadcastReceiverTwo添加abortBroadcast()拦截广播:
abortBroadcast();
Log.i("MyBroadcastReceiverTwo","我是广播接收者Two,广播被我终结了");
再次运行程序,结果如下:
可以看出,只有MyBroadcastReceiverTwo接收到了自定义的广播事件,其他的广播接收者都没有接收到,因此说明广播被MyBroadcastReceiverTwo终止了。