一、
1.有序广播
有序广播是一种同步执行的广播,在广播发出之后,同一时刻只会有一个广播接收器能够接收到这条消息,当这个广播接收器中的逻辑执行完毕后,广播才会继续传递。所以此时的广播接收器是有先后顺序的,并且可以被拦截。
2.创建应用程序
对应的布局文件 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.myapplication.MainActivity"> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送有序广播" android:layout_centerHorizontal="true" android:layout_marginTop="100dp" android:textSize="20sp" android:background="#FBFBFF" android:onClick="send"/> </RelativeLayout>3.在布局文件中为按钮Button注册了一个事件send,当点击按钮时,会发送一条有序广播。MainActivity的代码如下:
package cn.edu.bzu.myapplication; 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"); sendBroadcast(intent,null); } }4.添加三个广播接收者MyReceiverOne、MyReceiverTwo、MyReceiverThree。不同的广播接收者打印不同的提示信息。
(1)MyReceiverOne的代码如下:
package cn.edu.bzu.myapplication; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class MyReceiverOne extends BroadcastReceiver { public MyReceiverOne() { } @Override public void onReceive(Context context, Intent intent) { Log.i("MyReceiverOne","自定义的广播接收者one,接收到了广播事件"); } }
(2)MyReceiverTwo的代码如下:
package cn.edu.bzu.myapplication; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class MyReceiverTwo extends BroadcastReceiver { public MyReceiverTwo() { } @Override public void onReceive(Context context, Intent intent) { Log.i("MyReceiverTwo","自定义广播接收者Two,已接受到了广播事件"); } }
(3)MyReceiverThree的代码如下:
package cn.edu.bzu.myapplication; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class MyReceiverThree extends BroadcastReceiver { public MyReceiverThree() { } @Override public void onReceive(Context context, Intent intent) { Log.i("MyreceiverThree","自定义广播接收者Three,已接收到了广播事件"); } }
5.在清单文件中注册并为它们指定不同的优先级,代码如下:<receiver android:name="cn.edu.bzu.myapplication.MyReceiverOne" > <intent-filter android:priority="1000"> <action android:name="intercept_stitch"></action> </intent-filter> </receiver> <receiver android:name="cn.edu.bzu.myapplication.MyReceiverTwo" > <intent-filter android:priority="200"> <action android:name="intercept_stitch" ></action> </intent-filter> </receiver> <receiver android:name="cn.edu.bzu.myapplication.MyReceiverThree" > <intent-filter android:priority="600"> <action android:name="intercept_stitch" ></action> </intent-filter> </receiver>