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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:background="@drawable/stitch_one" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.administrator.xzx.MainActivity"> <Button android:layout_centerHorizontal="true" android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送有序广播" android:onClick="click"/> </RelativeLayout>2.编写MainActivity实现点击按钮处理
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View view){ Intent intent= new Intent(); intent.setAction("Intercept"); sendOrderedBroadcast(intent,null); } } 3.创建三个 广播接收者public class MyReceiver extends BroadcastReceiver { public MyReceiver() { } @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.i("MyReceiver","自定义的广播接收者,接都到了广播事件"); } }4.注册广播接收者<receiver android:name=".MyReceiver"> <intent-filter android:priority="1000"> <action android:name="Intercept" /> </intent-filter> </receiver> <receiver android:name=".MyReceiver2"> <intent-filter android:priority="2000"> <action android:name="Intercept" /> </intent-filter> </receiver> <receiver android:name=".MyReceiver3"> <intent-filter android:priority="600"> <action android:name="Intercept" /> </intent-filter> </receiver>5.运行程序得到
更改广播接收者2的priority值 使其优先级最高得到
说明广播被拦截并且后续广播无法继续进行