我们来看一下运行图:
有序广播(Ordered Broadcast):
一,优缺点
优点:1,按优先级的不同,优先Receiver可对数据进行处理,并传给下一个Receiver
2,通过abortBroadcast可终止广播的传播
缺点:效率低
二,发送广播的方法:sendOrderedBroadcast()
三,优先接收到Broadcast的Receiver可通过setResultExtras(Bundle)方法将处理结果存入Broadcast中,
下一个Receiver 通过 Bundle bundle=getResultExtras(true)方法获取上一个 Receiver传来的数据
一.布局界面
activity_order.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_order" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/stitch_one" tools:context="cn.edu.bzu.broadcast.OrderActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送有序广播" android:layout_centerHorizontal="true" android:background="#FBFBFF" android:textSize="20sp" android:layout_marginTop="50dp" android:onClick="send"/> </RelativeLayout> 二.Activity
1.OrderActivity
public class OrderActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_order); } public void send(View view){ Intent intent=new Intent(); intent.setAction("Intercept_Stitch"); sendOrderedBroadcast(intent,null); } }
2.FirstReceiver
public class FirstReceiver extends BroadcastReceiver { public FirstReceiver() { } @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.i("FirstReceiver","自定义广播接受者one,接收到广播事件"); } } 3.TwoReceiver
public class TwoReceiver extends BroadcastReceiver { public TwoReceiver() { } @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.i("TwoReceiver","自定义广播接受者two,接收到广播事件"); abortBroadcast(); Log.i("TwoReceiver","我的广播接受者two,广播被我终结了"); } } 3.ThreeReceiver
public class ThreeReceiver extends BroadcastReceiver { public ThreeReceiver() { } @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.i("ThreeReceiver","自定义广播接受者three,接收到广播事件"); } } 三.清单文件
AndroidManifest
<activity android:name=".OrderActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".FirstReceiver"> <intent-filter android:priority="1000"> <action android:name="Intercept_Stitch"></action> </intent-filter> </receiver> <receiver android:name=".TwoReceiver"> <intent-filter android:priority="200"> <action android:name="Intercept_Stitch"></action> </intent-filter> </receiver> <receiver android:name=".ThreeReceiver"> <intent-filter android:priority="600"> <action android:name="Intercept_Stitch"></action> </intent-filter> </receiver> 在这里面加上这些,priority代表优先级数越大优先级越高,就先发出广播,优先级相同时按顺序,在前面的优先级高。