Android 广播 - 静态注册与动态注册对广播接收器实例创建的影响

一、静态注册对广播接收器实例创建的影响

1、基本介绍
  • 静态注册的广播接收器,每次发送广播,都会新建一个广播接收器实例
2、演示
(1)Receiver
  • TestReceiver.java
java 复制代码
public class TestReceiver extends BroadcastReceiver {

    public static final String TAG = TestReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "收到内容 - " + this);
    }
}
  • AndroidManifest.xml
xml 复制代码
<receiver
    android:name=".mybroadcast.TestReceiver"
    android:exported="false" />
(2)Activity
  • activity_main.xml
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送广播"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
  • MainActivity.java
java 复制代码
public class MainActivity extends AppCompatActivity {

    public static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnSend = findViewById(R.id.btn_send);

        btnSend.setOnClickListener(v -> {
            Intent intent = new Intent();
            ComponentName component = new ComponentName(this, TestReceiver.class);
            intent.setComponent(component);
            sendBroadcast(intent);
        });
    }
}
(3)Test
  1. 第 1 次点击按钮,发送广播,输出结果如下

    收到内容 - com.my.broadcast.mybroadcast.TestReceiver@1a66a22

  2. 第 2 次点击按钮,发送广播,输出结果如下

    收到内容 - com.my.broadcast.mybroadcast.TestReceiver@58d710f


二、动态注册对广播接收器实例创建的影响

1、基本介绍
  • 动态注册的广播接收器,每次发送广播,只有一个广播接收器实例
2、演示
(1)Receiver
java 复制代码
public class TestReceiver extends BroadcastReceiver {

    public static final String TAG = TestReceiver.class.getSimpleName();

    public static final String ACTION = TAG;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "收到内容 - " + this);
    }
}
(2)Activity
  • activity_main.xml
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送广播"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
  • MainActivity.java
java 复制代码
public class MainActivity extends AppCompatActivity {

    public static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TestReceiver receiver = new TestReceiver();
        IntentFilter intentFilter = new IntentFilter(TestReceiver.ACTION);
        registerReceiver(receiver, intentFilter);

        Button btnSend = findViewById(R.id.btn_send);

        btnSend.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setAction(TestReceiver.ACTION);
            sendBroadcast(intent);
        });
    }
}
(3)Test
  1. 第 1 次点击按钮,发送广播,输出结果如下

    收到内容 - com.my.broadcast.mybroadcast.TestReceiver@6458b62

  2. 第 2 次点击按钮,发送广播,输出结果如下

    收到内容 - com.my.broadcast.mybroadcast.TestReceiver@6458b62


三、在 Application 中动态注册

1、基本介绍
  • 在 Application 的 onCreate 方法中采用动态注册来注册广播接收器,只会创建一个广播接收器实例
2、演示
(1)Receiver
java 复制代码
public class TestReceiver extends BroadcastReceiver {

    public static final String TAG = TestReceiver.class.getSimpleName();

    public static final String ACTION = TAG;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "收到内容 - " + this);
    }
}
(2)Application
  • MyApplication.java
java 复制代码
public class MyApplication extends Application {

    private TestReceiver testReceiver;

    @Override
    public void onCreate() {
        super.onCreate();

        testReceiver = new TestReceiver();
        IntentFilter filter = new IntentFilter(TestReceiver.ACTION);
        registerReceiver(testReceiver, filter);
    }
}
(3)Activity
  • activity_main.xml
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送广播"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
  • MainActivity.java
java 复制代码
public class MainActivity extends AppCompatActivity {

    public static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnSend = findViewById(R.id.btn_send);

        btnSend.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setAction(TestReceiver.ACTION);
            sendBroadcast(intent);
        });
    }
}
(4)Test
  1. 第 1 次点击按钮,发送广播,输出结果如下

    收到内容 - com.my.broadcast.mybroadcast.TestReceiver@332c7bc

  2. 第 2 次点击按钮,发送广播,输出结果如下

    收到内容 - com.my.broadcast.mybroadcast.TestReceiver@332c7bc


四、在 Activity 中动态注册

1、基本介绍
  1. 如果在 Activity 中采用动态注册来注册广播接收器,需要在合适的时机注销广播接收器,否则会创建多个广播接收器实例

  2. 如果存在多个广播接收器实例,它们会同时接收广播

2、多个广播接收器实例
(1)Receiver
java 复制代码
public class TestReceiver extends BroadcastReceiver {

    public static final String TAG = TestReceiver.class.getSimpleName();

    public static final String ACTION = TAG;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "收到内容 - " + this);
    }
}
(2)Activity
  • activity_main.xml
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送广播"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
  • MainActivity.java
java 复制代码
public class MainActivity extends AppCompatActivity {

    public static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        registerReceiver(new TestReceiver(), new IntentFilter(TestReceiver.ACTION));
        registerReceiver(new TestReceiver(), new IntentFilter(TestReceiver.ACTION));

        Button btnSend = findViewById(R.id.btn_send);

        btnSend.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setAction(TestReceiver.ACTION);
            sendBroadcast(intent);
        });
    }
}
(3)Test
  1. 第 1 次点击按钮,发送广播,输出结果如下

    收到内容 - com.my.broadcast.mybroadcast.TestReceiver@6458b62
    收到内容 - com.my.broadcast.mybroadcast.TestReceiver@be064f3

  2. 第 2 次点击按钮,发送广播,输出结果如下

    收到内容 - com.my.broadcast.mybroadcast.TestReceiver@6458b62
    收到内容 - com.my.broadcast.mybroadcast.TestReceiver@be064f3

相关推荐
小bo波13 小时前
使用Thread子类创建线程 VS 使用Runnable接口创建线程的区别
java·多线程·thread·并发编程·runnable
SamDeepThinking13 小时前
高并发场景下,CompletableFuture与ForkJoinPool该如何取舍?
java·后端·面试
方白羽16 小时前
Android Gradle 缓存与文件目录深度解析
android·gradle·android studio
张不才16 小时前
CPU 100% 了怎么办?Java 性能排障的标准化操作
java·后端
shepherd11117 小时前
吞吐量提升 10 倍:高并发大批量数据处理任务的架构演进与性能调优
java·后端·架构
曲幽19 小时前
Termux里的二进制和脚本,到底怎么运行才不踩坑?Termux-service 保活妙招!
android·termux·nohup·services·wake-lock
plainGeekDev20 小时前
单例模式 → object 声明
android·java·kotlin
程序员陆业聪21 小时前
读者点单·03|Compose 与传统 View 混用的 12 个真实坑
android
程序员陆业聪21 小时前
读者点单·02|Android 启动优化实战:Trace 抓取→Application 编排→冷启动全流程拆解
android
Coffeeee21 小时前
帮你快速理解AI Agent之我想招个Android实习生
android·人工智能·agent