文章标题 拦截有序广播

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

<?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>

效果图

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值