有序广播是一种同步执行的广播,在广播发出之后,同一时刻只会有一个广播接收器能够接收到这条消息,当这个广播接收器中的逻辑执行完毕后,广播才会继续传递,所以,此时的广播接收器是有先后顺序的,并且可以被拦截。
现在我们通过sendOrderedBroadcast()方法发送一条有序广播,其中并设置广播接收者的优先级,创建3个广播接受者,具体代码如下:
首先创建一个名为endOrderedBroadcast的应用程序,将包名修改为cn.edu.bzu.endOrderedBroadcast。
布局文件如下:
<?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:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:onClick="send" android:text="发送有序广播" android:paddingLeft="5dp" android:paddingRight="5dp" android:background="#FBFBFF" android:textSize="20sp" android:layout_marginTop="80dp" android:id="@+id/button" /> </RelativeLayout>
Mainactivity的代码如下:
package cn.edu.bzu.orderedbroadcast; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; 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"); sendBroadcast(intent,null); } }
添加广播接收者:
广播接受者MyBroadcastReceiverOne:
package cn.edu.bzu.orderedbroadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class MyBroadcastReceiverOne extends BroadcastReceiver { public MyBroadcastReceiverOne() { } @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. 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; public class MyBroadcastReceiverTwo extends BroadcastReceiver { public MyBroadcastReceiverTwo() { } @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.i("MyBroadcastReceiverTwo","自定义的广播接受者Two,接受到了广播事件"); } }广播接受者MyBroadcastReceiverThree:package cn.edu.bzu.orderedbroadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class MyBroadcastReceiverThree extends BroadcastReceiver { public MyBroadcastReceiverThree() { } @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.i("MyBroadcastReceiverThree","自定义的广播接收者Three,接收到了广播事件"); } }然后设置广播接受者的优先级:<?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>最后结果如下: