有序广播案例实现

本文详细介绍了如何在Android中实现有序广播,并探讨了广播接收者的接收顺序,特别是当广播在传递过程中被拦截时的影响。示例代码包括布局文件的设置和广播接收器的编写,展示了如何通过优先级和注册顺序控制广播接收的顺序。通过实例展示了一个有序广播在被拦截后,后续接收者无法接收到广播的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过设置不同的优先级查看广播接受者的接受顺序,或通过不同的注册顺序查看广播接受者的接受顺序

广播的接受是按照事先设定的优先级接收的
优先级相同的广播接收者先被注册的先接收广播
如果广播在传递过程中被拦截,则后面的广播则无法接受广播

我们首先先写基本的布局文件和广播接收器


布局文件

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




package com.example.bz0209.myapplication_3;

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


package com.example.bz0209.myapplication_3;

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) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Log.i("one", "one 接收到了事件");
    }
}

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bz0209.myapplication_3">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".MyReceiverone"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="1000">
                <action android:name="Intercept_Stitch"/>
            </intent-filter>
        </receiver>
        <receiver
            android:name=".MyReceivertwo"
            android:enabled="true"
            android:exported="true" >
            <intent-filter android:priority="200">
                <action android:name="Intercept_Stitch"/>
            </intent-filter>
        </receiver>
        <receiver
            android:name=".MyReceiverthree"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="600">
                <action android:name="Intercept_Stitch"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>



注意上面的Manifest文件中不同receiver的优先级
1. 正常运行测试:

我们通过日志的打印信息可以看到广播的接受是按照事先设定的优先级接收的。
2. 我们把MyReceivertwo的优先级同样设置成为1000并且把注册信息移动到MyReceiverone前面,效果如图

我们发现优先级相同的广播接收者先被注册的先接收广播
3. 我们把MyReceivertwo修改为下面图片:


我们看到广播在MyReceivertwo被拦截,所以MyReceiverone和MyReceiverthree接收不到我广播广播



效果如图:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值