赞
踩
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/stitch_one"
- tools:context="com.example.broadcast.MainActivity">
-
- <Button
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:onClick="send"
- android:layout_marginTop="50dp"
- android:layout_marginLeft="110dp"
- android:textSize="20sp"
- android:text="发送有序广播"
- />
-
- </RelativeLayout>

MainActivity的代码如下:
2、添加广播接收者
package com.example.broadcast; 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); } }
MyBroadcastReceiverOne,MyBroadcastReceiverTwo,
MyBroadcastReceiverThree不同的接收者打印不同的提示信息。
广播接收者MyBroadcastReceiverOne的代如下:
- package com.example.broadcast;
-
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
-
- public class MyBroadcastReceiverOne extends BroadcastReceiver {
-
- @Override
- public void onReceive(Context context, Intent intent) {
- Log.i("MyBroadcastReceiverOne","自定义的广播接收者One,接收到了广播事件");
-
- }
- }
广播接收者MyBroadcastReceiverTwo的代如下:
- package com.example.broadcast;
-
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
-
- public class MyBroadcastReceiverTwo extends BroadcastReceiver {
-
- @Override
- public void onReceive(Context context, Intent intent) {
- Log.i("MyBroadcastReceiverTwo","自定义的广播接收者Two,接收到了广播事件");
-
- }
- }
广播接收者MyBroadcastReceiverThree的代如下:
- package com.example.broadcast;
-
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
-
- public class MyBroadcastReceiverThree extends BroadcastReceiver {
-
- @Override
- public void onReceive(Context context, Intent intent) {
- Log.i("MyBroadcastReceiverThree","自定义的广播接收者Three,接收到了广播事件");
- }
- }
三个广播接收者创建成功之后,需要在清单文件中注册他们并为它们制定不同的优先级,集体代码如下所示:
- <receiver
- android:name=".MyBroadcastReceiverOne"
- android:enabled="true"
- android:exported="true">
- <intent-filter android:priority="1000">
- <action android:name="Intercept_Stitch" />
- </intent-filter>
- </receiver>
- <receiver
- android:name=".MyBroadcastReceiverTwo"
- android:enabled="true"
- android:exported="true" >
- <intent-filter android:priority="200">
- <action android:name="Intercept_Stitch" />
- </intent-filter>
- </receiver>
-
-
- <receiver
- android:name=".MyBroadcastReceiverThree"
- android:enabled="true"
- android:exported="true">
- <intent-filter android:priority="600">
- <action android:name="Intercept_Stitch" />
- </intent-filter>
- </receiver>

3、运行程序拦截有序广播
修改MyBroadcastReceiverTwo如下,观察结果,你又可以得出什么结论?
package com.example.broadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class MyBroadcastReceiverTwo extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.i("MyBroadcastReceiverTwo","自定义的广播接收者Two,接收到了广播事件"); abortBroadcast(); Log.i("MyBroadcastReceiverTwo","自定义的广播接收者Two,广播被我终结啦"); } }
结果:
结论:abortBroadcast()语句用于终止有序广播,当程序执行完该句后,广播事件将会终结,不会再向下传递。
MyBroadcastReceiverTwo的代如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。