文章标题 拦截有序广播

来源:互联网 发布:巴黎文华东方酒店知乎 编辑:程序博客网 时间:2024/05/29 08:29

拦截有序广播程序对应的布局文件

<?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.bordercast3.MainActivity"><Button    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_centerHorizontal="true"    android:layout_marginTop="80dp"    android:onClick="send"    android:background="#f2d2f1f4"    android:text="发送有序广播"    android:textSize="20sp"/></RelativeLayout>

上述布局文件中,定义一个Button按钮,并为按钮注册了点击事件send,当用户点击该按钮时,会发送一条有序广播。

MainActivitty代码

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);    }}

sendOrderBordercast(intent,null)用于发送一个有序广播,第一个参数指定意图,设置要发送的广播事件,第二个参数制定接受者的权限,如果不想让所有的接受者都看到,可以显式的指定接受者,不关心权限可以将其指定为null。

添加广播接收者

广播接收者1

public class MyReceiverone extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        // TODO: This method is called when the BroadcastReceiver is receiving        // an Intent broadcast.        Log.i(" MyReceiverone","自定义广播接收者one,接受到了广播事件");    }}

广播接收者2

public class MyReceiverTwo extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        // TODO: This method is called when the BroadcastReceiver is receiving        // an Intent broadcast.        Log.i(" MyReceivertwo","自定义广播接收者two,接受到了广播事件");    }}

广播接收者3

public class MyReceiverthree extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        // TODO: This method is called when the BroadcastReceiver is receiving        // an Intent broadcast.        Log.i(" MyReceiverthree","自定义广播接收者three,接受到了广播事件");    }}

清单文件

 <receiver android:name=".MyReceiverone">            <intent-filter android:priority="1000">                <action android:name="Intercept_Stitch"/>            </intent-filter>        </receiver>        <receiver android:name=".MyReceiverTwo">        <intent-filter android:priority="200">            <action android:name="Intercept_Stitch"/>        </intent-filter>        </receiver>        <receiver android:name=".MyReceiverthree">            <intent-filter android:priority="600">                <action android:name="Intercept_Stitch"/>            </intent-filter>        </receiver>

运行效果

修改MyBroadcastReceiverTwo如下

public class MyReceiverTwo extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        // TODO: This method is called when the BroadcastReceiver is receiving        // an Intent broadcast.        Log.i(" MyReceivertwo","自定义广播接收者two,接受到了广播事件");       abortBroadcast();        Log.i(" MyReceivertwo","我是广播接收者two,广告被我终结了");    }}

效果

若将广播接收者MyBroadcastReceiverTwo优先级同样设置为1000,并将MyBroadcastReceiverTwo注册在MyBroadcastReceiverOne前面

 <receiver android:name=".MyReceiverTwo">            <intent-filter android:priority="1000">                <action android:name="Intercept_Stitch"/>            </intent-filter>        </receiver>        <receiver android:name=".MyReceiverone">            <intent-filter android:priority="1000">                <action android:name="Intercept_Stitch"/>            </intent-filter>        </receiver>        <receiver android:name=".MyReceiverthree">            <intent-filter android:priority="600">                <action android:name="Intercept_Stitch"/>            </intent-filter>        </receiver>

效果图

广播的优先级相同时,先注册的广播会先接受广播事件

原创粉丝点击