您的当前位置:首页正文

Android中的广播机制(案例:模拟广播接收者与发送者+代码+效果图)

2024-11-17 来源:个人技术集锦

1.Android中的广播机制概念

1)广播接收器 (BroadcastReceiver)

  • BroadcastReceiver 是一个系统组件,用来接收并处理来自其他应用或系统的广播消息。
  • 它可以被静态注册(在 AndroidManifest.xml 文件中声明)或者动态注册(在代码中注册)。
  • 当接收到广播时,onReceive(Context, Intent) 方法会被调用,该方法负责处理广播内容。

2)注册 BroadcastReceiver

  • 静态注册:通过在 AndroidManifest.xml 文件中声明 <receiver> 标签来完成。这种方式使得即使应用运行,也接收到特定的广播
  • 动态注册:在运行时通过调用 Context.registerReceiver() 方法来注册广播接收器。这种方式通常在 Activity 或者 Service 中使用,并且需要在相应的生命周期方法中进行注册注销操作。

3)发送广播

  • 可以通过 sendBroadcast()sendOrderedBroadcast()sendStickyBroadcast() 等方法发送广播。
  • 标准广播 (sendBroadcast()) 会同时向所有匹配的接收者发送广播。
  • 有序广播 (sendOrderedBroadcast()) 按照优先级顺序依次发送给接收者,前一个接收者可以修改广播的内容,甚至可以完全终止广播继续传播
  • 粘性广播 (sendStickyBroadcast()) 在 Android 5.0 之后已被弃用,因为它会导致内存泄漏等问题。

4)广播意图 (Intent)

  • 广播是通过 Intent 对象来携带信息的。Intent 可以包含动作(action)、数据(data)、类别(category)以及其他额外的数据。
  • 接收者可以通过设置 IntentFilter 来过滤想要监听的广播类型

5)广播的生命周期

  • 广播接收者的生命周期非常短暂,一般只在 onReceive() 方法执行期间有效。
  • 如果 onReceive() 需要执行耗时操作,应该考虑将工作交给后台线程或者启动服务来处理。

6)安全性和权限

  • 应用可以通过设置权限来限制哪些应用可以发送特定类型的广播。
  • 从 Android 8.0 开始,对于隐式广播(即未指定目标组件的广播),除了少数例外情况外,不允许在清单文件中声明接收器。这类广播应该通过上下文注册的方式进行监听。

7)典型应用场景

  • 监听系统事件,例如设备充电状态变化、网络连接状态变化等。
  • 实现跨进程通信。
  • 启动应用或执行某些操作,比如开机启动时初始化应用状态。

2.创建广播接收者

1--使用Android-studio创建

找到自己应用的包===>鼠标右键===>new ===> Other ===>Broadcast Receiver

输入名字===>点击finsh按钮 ==》等在新的类生成

这是类中的内容

package com.xiji.myapplicationttt;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

并且会在AndroidManifest.xml文件中静态注册,手动创建的广播接收者并不会自动静态注册

2--手动创建

创建一个类,并且继承BroadcastReceiver

package com.xiji.myapplicationttt;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyDivBroadcastReceiver extends BroadcastReceiver {
    
    //重写接受方法
    @Override
    public void onReceive(Context context, Intent intent) {
        
        System.out.println("MyDivBroadcastReceiver onReceive");
    }
}

3.注册广播接收者

1--动态注册

package com.xiji.myapplicationttt;

import android.content.IntentFilter;
import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {
    private MyDivBroadcastReceiver mDivReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        //定义广播接收者
        mDivReceiver  = new MyDivBroadcastReceiver();
        String action = "myAction";

        //广播过滤
        IntentFilter filter = new IntentFilter();
        filter.addAction(action);
        
        //注册
        registerReceiver(mDivReceiver, filter);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //销毁注册
        unregisterReceiver(mDivReceiver);
    }
}

2--静态注册

<receiver
    android:name=".MyDivBroadcastReceiver"
    android:enabled="true"
    android:exported="true"></receiver>

完整的AndroidManifest.xml代码

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

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplicationttt"
        tools:targetApi="31">

        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true"></receiver>

        <receiver
            android:name=".MyDivBroadcastReceiver"
            android:enabled="true"
            android:exported="true"></receiver>


        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

4.案例:模拟广播接收者与发送者

1.activity_main.xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="广播发送者与接收者"
        android:layout_gravity="center"
        android:textSize="20dp"
        android:textColor="#DFECEF"
        android:background="#7BD113"
        android:gravity="center"
        android:padding="20dp"
        android:layout_marginBottom="30dp"
         />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginBottom="20dp"
        android:padding="20dp"
        android:background="#3AD3E7"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#BBD2E9"
            android:gravity="center"
            android:text="消息推送站"
            android:textColor="#124578"
            android:textSize="20dp"
            />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginBottom="20dp"
            android:padding="20dp"
            android:background="#DCF3F6"
            >
            <Button
                android:id="@+id/buttonSendMessage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:theme="@style/Theme.Design.NoActionBar"

                android:text="发送消息" />
            <EditText
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint="输入要发送的通知"
                android:inputType="text"
                android:id="@+id/editText"
                android:textColor="#456789"

                />
        </LinearLayout>

    </LinearLayout>



    <!--定义三个广播接受者-->
    <!--第一个广播接受者-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginBottom="20dp"
        >
        <ImageView
            android:layout_width="128dp"
            android:layout_height="128dp"
            android:src="@drawable/a_girl"
            android:id="@+id/imageViewOne"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:id="@+id/firstTextView"
            android:text="等待接受消息"
            />


    </LinearLayout>

    <!--第二个广播接受者-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginBottom="20dp"
        >
        <ImageView
            android:layout_width="128dp"
            android:layout_height="128dp"
            android:src="@drawable/girl_one"

            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:id="@+id/secondTextView"
            android:text="等待接受消息"
            />


    </LinearLayout>

    <!--第三个广播接受则-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <ImageView
            android:layout_width="128dp"
            android:layout_height="128dp"
            android:src="@drawable/boy_two"

            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:id="@+id/thirdTextView"
            android:text="等待接受消息"
            />


    </LinearLayout>


</LinearLayout>

2.MainActivity代码

package com.xiji.myapplicationttt;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {
    private MyDivBroadcastReceiver mDivReceiver;

    //定义三个接受者
    private ReceiverMessage mReceiverMessage;
    private ReceiverMessageTwo mReceiverMessageTwo;
    private ReceiverMessageThree mReceiverMessageThree;

    //发送事件
    private Button buttonSendMessage;
    //编辑消息
    private EditText editTextMessage;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        initListener();
        initSubscriber();


    }
    //订阅者初始化
    private void initSubscriber() {
        //定义广播接收者
        String action  = "message";

        mReceiverMessage = new ReceiverMessage();

        mReceiverMessageTwo = new ReceiverMessageTwo();

        mReceiverMessageThree = new ReceiverMessageThree();

        //为每个接受者设置过滤器
        IntentFilter filter = new IntentFilter(action);
        filter.addAction(action);

        IntentFilter filterTwo = new IntentFilter(action);
        filterTwo.addAction(action);

        IntentFilter filterThree = new IntentFilter(action);
        filterThree.addAction(action);

        //注册广播接收者
        registerReceiver(mReceiverMessage, filter);
        registerReceiver(mReceiverMessageTwo, filterTwo);
        registerReceiver(mReceiverMessageThree, filterThree);


        //注册
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //销毁注册

        unregisterReceiver(mReceiverMessage);
        unregisterReceiver(mReceiverMessageTwo);
        unregisterReceiver(mReceiverMessageThree);
    }


    //控件监听初始化
    private void initListener() {
        buttonSendMessage = findViewById(R.id.buttonSendMessage);
        editTextMessage = findViewById(R.id.editText);

        buttonSendMessage.setOnClickListener(view -> {
            //发送消息
            String message = editTextMessage.getText().toString();
            Intent intent = new Intent();
            intent.setAction("message");

            intent.putExtra("myMessage", message);
            sendOrderedBroadcast(intent, null);
        });
    }





//延迟500ms
    private void delay() {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }








    //内部类  == >内部类可以调用外部类的成员 ==》获取控件
    class ReceiverMessage extends BroadcastReceiver {
        //显示控件
        private TextView firstTextView;
        @Override
        public void onReceive(Context context, Intent intent) {
            //通知信息
            Log.i("ReceiverMessageOne", "收到广播信息" + intent.getStringExtra("myMessage").toString());

            firstTextView = findViewById(R.id.firstTextView);
            //获取额外信息

            firstTextView.setText("小久接受到消息:"+intent.getStringExtra("myMessage"));
            delay();

        }


    }


    class ReceiverMessageTwo extends BroadcastReceiver {
        private TextView secondTextView;

        @Override
        public void onReceive(Context context, Intent intent) {

            Log.i("ReceiverMessageTwo", "收到广播信息" + intent.getStringExtra("myMessage"));
            secondTextView = findViewById(R.id.secondTextView);
            secondTextView.setText("张三接受到消息:"+intent.getStringExtra("myMessage").toString());
            delay();
        }
    }

    class ReceiverMessageThree extends BroadcastReceiver {

        private TextView thirdTextView;
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("ReceiverMessageThree", "收到广播信息" + intent.getStringExtra("myMessage").toString());
            thirdTextView = findViewById(R.id.thirdTextView);
            thirdTextView.setText("惜.己接受到消息:"+intent.getStringExtra("myMessage"));
            delay();
        }
    }


}

3.效果

1--有序推送效果

2--优先级设置setPriority

1---修改代码
setPriority值越大优先级越高

filterTwo ===> filterThree ==>filter

//设置优先级  值越大优先级越高
filter.setPriority(100);

filterTwo.setPriority(300);
filterThree.setPriority(150);
2----完整的MainAcivity
package com.xiji.myapplicationttt;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {
    private MyDivBroadcastReceiver mDivReceiver;

    //定义三个接受者
    private ReceiverMessage mReceiverMessage;
    private ReceiverMessageTwo mReceiverMessageTwo;
    private ReceiverMessageThree mReceiverMessageThree;

    //发送事件
    private Button buttonSendMessage;
    //编辑消息
    private EditText editTextMessage;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        initListener();
        initSubscriber();


    }
    //订阅者初始化
    private void initSubscriber() {
        //定义广播接收者
        String action  = "message";

        mReceiverMessage = new ReceiverMessage();

        mReceiverMessageTwo = new ReceiverMessageTwo();

        mReceiverMessageThree = new ReceiverMessageThree();

        //为每个接受者设置过滤器
        IntentFilter filter = new IntentFilter(action);
        filter.addAction(action);


        IntentFilter filterTwo = new IntentFilter(action);
        filterTwo.addAction(action);

        IntentFilter filterThree = new IntentFilter(action);
        filterThree.addAction(action);


        //设置优先级  值越大优先级越高
        filter.setPriority(100);

        filterTwo.setPriority(300);
        filterThree.setPriority(150);

        //注册广播接收者
        registerReceiver(mReceiverMessage, filter);
        registerReceiver(mReceiverMessageTwo, filterTwo);
        registerReceiver(mReceiverMessageThree, filterThree);


        //注册
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //销毁注册

        unregisterReceiver(mReceiverMessage);
        unregisterReceiver(mReceiverMessageTwo);
        unregisterReceiver(mReceiverMessageThree);
    }


    //控件监听初始化
    private void initListener() {
        buttonSendMessage = findViewById(R.id.buttonSendMessage);
        editTextMessage = findViewById(R.id.editText);

        buttonSendMessage.setOnClickListener(view -> {
            //发送消息
            String message = editTextMessage.getText().toString();
            Intent intent = new Intent();
            intent.setAction("message");

            intent.putExtra("myMessage", message);

            sendOrderedBroadcast(intent, null);

            //无序推送
//            sendBroadcast(intent);
        });
    }





//延迟500ms
    private void delay() {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }








    //内部类  == >内部类可以调用外部类的成员 ==》获取控件
    class ReceiverMessage extends BroadcastReceiver {
        //显示控件
        private TextView firstTextView;
        @Override
        public void onReceive(Context context, Intent intent) {
            //通知信息
            Log.i("ReceiverMessageOne", "收到广播信息" + intent.getStringExtra("myMessage").toString());

            firstTextView = findViewById(R.id.firstTextView);
            //获取额外信息

            firstTextView.setText("小久接受到消息:"+intent.getStringExtra("myMessage"));
            delay();

        }


    }


    class ReceiverMessageTwo extends BroadcastReceiver {
        private TextView secondTextView;

        @Override
        public void onReceive(Context context, Intent intent) {

            Log.i("ReceiverMessageTwo", "收到广播信息" + intent.getStringExtra("myMessage"));
            secondTextView = findViewById(R.id.secondTextView);
            secondTextView.setText("张三接受到消息:"+intent.getStringExtra("myMessage").toString());
            delay();
        }
    }

    class ReceiverMessageThree extends BroadcastReceiver {

        private TextView thirdTextView;
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("ReceiverMessageThree", "收到广播信息" + intent.getStringExtra("myMessage").toString());
            thirdTextView = findViewById(R.id.thirdTextView);
            thirdTextView.setText("惜.己接受到消息:"+intent.getStringExtra("myMessage"));
            delay();
        }
    }


}
3---优先级推送效果

2 ===> 3 ==> 1

3--消息拦截abortBroadcast()

1---修改拦截

abortBroadcast() //在onReceiver中添加

在三号ReceiverMessageThree添加拦截

    class ReceiverMessageThree extends BroadcastReceiver {

        private TextView thirdTextView;
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("ReceiverMessageThree", "收到广播信息" + intent.getStringExtra("myMessage").toString());
            thirdTextView = findViewById(R.id.thirdTextView);
            thirdTextView.setText("惜.己接受到消息:"+intent.getStringExtra("myMessage"));
            //三号添加拦截
            abortBroadcast();
            delay();
        }
    }
2---完整代码
package com.xiji.myapplicationttt;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {
    private MyDivBroadcastReceiver mDivReceiver;

    //定义三个接受者
    private ReceiverMessage mReceiverMessage;
    private ReceiverMessageTwo mReceiverMessageTwo;
    private ReceiverMessageThree mReceiverMessageThree;

    //发送事件
    private Button buttonSendMessage;
    //编辑消息
    private EditText editTextMessage;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        initListener();
        initSubscriber();


    }
    //订阅者初始化
    private void initSubscriber() {
        //定义广播接收者
        String action  = "message";

        mReceiverMessage = new ReceiverMessage();

        mReceiverMessageTwo = new ReceiverMessageTwo();

        mReceiverMessageThree = new ReceiverMessageThree();

        //为每个接受者设置过滤器
        IntentFilter filter = new IntentFilter(action);
        filter.addAction(action);


        IntentFilter filterTwo = new IntentFilter(action);
        filterTwo.addAction(action);

        IntentFilter filterThree = new IntentFilter(action);
        filterThree.addAction(action);


        //设置优先级  值越大优先级越高
        filter.setPriority(100);

        filterTwo.setPriority(300);
        filterThree.setPriority(150);

        //注册广播接收者
        registerReceiver(mReceiverMessage, filter);
        registerReceiver(mReceiverMessageTwo, filterTwo);
        registerReceiver(mReceiverMessageThree, filterThree);


        //注册
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //销毁注册

        unregisterReceiver(mReceiverMessage);
        unregisterReceiver(mReceiverMessageTwo);
        unregisterReceiver(mReceiverMessageThree);
    }


    //控件监听初始化
    private void initListener() {
        buttonSendMessage = findViewById(R.id.buttonSendMessage);
        editTextMessage = findViewById(R.id.editText);

        buttonSendMessage.setOnClickListener(view -> {
            //发送消息
            String message = editTextMessage.getText().toString();
            Intent intent = new Intent();
            intent.setAction("message");

            intent.putExtra("myMessage", message);

            sendOrderedBroadcast(intent, null);

            //无序推送
//            sendBroadcast(intent);
        });
    }





//延迟500ms
    private void delay() {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }








    //内部类  == >内部类可以调用外部类的成员 ==》获取控件
    class ReceiverMessage extends BroadcastReceiver {
        //显示控件
        private TextView firstTextView;
        @Override
        public void onReceive(Context context, Intent intent) {
            //通知信息
            Log.i("ReceiverMessageOne", "收到广播信息" + intent.getStringExtra("myMessage").toString());

            firstTextView = findViewById(R.id.firstTextView);
            //获取额外信息

            firstTextView.setText("小久接受到消息:"+intent.getStringExtra("myMessage"));
            delay();

        }


    }


    class ReceiverMessageTwo extends BroadcastReceiver {
        private TextView secondTextView;

        @Override
        public void onReceive(Context context, Intent intent) {

            Log.i("ReceiverMessageTwo", "收到广播信息" + intent.getStringExtra("myMessage"));
            secondTextView = findViewById(R.id.secondTextView);
            secondTextView.setText("张三接受到消息:"+intent.getStringExtra("myMessage").toString());




            delay();
        }
    }

    class ReceiverMessageThree extends BroadcastReceiver {

        private TextView thirdTextView;
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("ReceiverMessageThree", "收到广播信息" + intent.getStringExtra("myMessage").toString());
            thirdTextView = findViewById(R.id.thirdTextView);
            thirdTextView.setText("惜.己接受到消息:"+intent.getStringExtra("myMessage"));
            //三号添加拦截
            abortBroadcast();
            delay();
        }
    }


}
3---效果

推送顺序还是2==》3===》1 

可以看到消息在3的时候被拦截,没有向后传递

4--无序推送

1---修改推送
//无序推送
sendBroadcast(intent);
2---完整的MainAtivity代码
package com.xiji.myapplicationttt;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {
    private MyDivBroadcastReceiver mDivReceiver;

    //定义三个接受者
    private ReceiverMessage mReceiverMessage;
    private ReceiverMessageTwo mReceiverMessageTwo;
    private ReceiverMessageThree mReceiverMessageThree;

    //发送事件
    private Button buttonSendMessage;
    //编辑消息
    private EditText editTextMessage;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        initListener();
        initSubscriber();


    }
    //订阅者初始化
    private void initSubscriber() {
        //定义广播接收者
        String action  = "message";

        mReceiverMessage = new ReceiverMessage();

        mReceiverMessageTwo = new ReceiverMessageTwo();

        mReceiverMessageThree = new ReceiverMessageThree();

        //为每个接受者设置过滤器
        IntentFilter filter = new IntentFilter(action);
        filter.addAction(action);


        IntentFilter filterTwo = new IntentFilter(action);
        filterTwo.addAction(action);

        IntentFilter filterThree = new IntentFilter(action);
        filterThree.addAction(action);


        //设置优先级  值越大优先级越高
        filter.setPriority(100);

        filterTwo.setPriority(300);
        filterThree.setPriority(150);

        //注册广播接收者
        registerReceiver(mReceiverMessage, filter);
        registerReceiver(mReceiverMessageTwo, filterTwo);
        registerReceiver(mReceiverMessageThree, filterThree);


        //注册
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //销毁注册

        unregisterReceiver(mReceiverMessage);
        unregisterReceiver(mReceiverMessageTwo);
        unregisterReceiver(mReceiverMessageThree);
    }


    //控件监听初始化
    private void initListener() {
        buttonSendMessage = findViewById(R.id.buttonSendMessage);
        editTextMessage = findViewById(R.id.editText);

        buttonSendMessage.setOnClickListener(view -> {
            //发送消息
            String message = editTextMessage.getText().toString();
            Intent intent = new Intent();
            intent.setAction("message");

            intent.putExtra("myMessage", message);

//            sendOrderedBroadcast(intent, null);

            //无序推送
            sendBroadcast(intent);
        });
    }





//延迟500ms
    private void delay() {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }








    //内部类  == >内部类可以调用外部类的成员 ==》获取控件
    class ReceiverMessage extends BroadcastReceiver {
        //显示控件
        private TextView firstTextView;
        @Override
        public void onReceive(Context context, Intent intent) {
            //通知信息
            Log.i("ReceiverMessageOne", "收到广播信息" + intent.getStringExtra("myMessage").toString());

            firstTextView = findViewById(R.id.firstTextView);
            //获取额外信息

            firstTextView.setText("小久接受到消息:"+intent.getStringExtra("myMessage"));
            delay();

        }


    }


    class ReceiverMessageTwo extends BroadcastReceiver {
        private TextView secondTextView;

        @Override
        public void onReceive(Context context, Intent intent) {

            Log.i("ReceiverMessageTwo", "收到广播信息" + intent.getStringExtra("myMessage"));
            secondTextView = findViewById(R.id.secondTextView);
            secondTextView.setText("张三接受到消息:"+intent.getStringExtra("myMessage").toString());




            delay();
        }
    }

    class ReceiverMessageThree extends BroadcastReceiver {

        private TextView thirdTextView;
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("ReceiverMessageThree", "收到广播信息" + intent.getStringExtra("myMessage").toString());
            thirdTextView = findViewById(R.id.thirdTextView);
            thirdTextView.setText("惜.己接受到消息:"+intent.getStringExtra("myMessage"));
            //三号添加拦截
            abortBroadcast();
            delay();
        }
    }


}

3----无序推送效果

注:我们在代码中设置的有消息拦截,但是无序推送无法被拦截

无序推送无法被拦截,速度快,

显示全文