news 2026/6/10 12:32:12

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Android 广播 - 静态注册与动态注册对广播接收器实例创建的影响

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

1、基本介绍
  • 静态注册的广播接收器,每次发送广播,都会新建一个广播接收器实例
2、演示
(1)Receiver
  • TestReceiver.java
publicclassTestReceiverextendsBroadcastReceiver{publicstaticfinalStringTAG=TestReceiver.class.getSimpleName();@OverridepublicvoidonReceive(Contextcontext,Intentintent){Log.i(TAG,"收到内容 - "+this);}}
  • AndroidManifest.xml
<receiverandroid:name=".mybroadcast.TestReceiver"android:exported="false"/>
(2)Activity
  • activity_main.xml
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns: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"><Buttonandroid: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
publicclassMainActivityextendsAppCompatActivity{publicstaticfinalStringTAG=MainActivity.class.getSimpleName();@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ButtonbtnSend=findViewById(R.id.btn_send);btnSend.setOnClickListener(v->{Intentintent=newIntent();ComponentNamecomponent=newComponentName(this,TestReceiver.class);intent.setComponent(component);sendBroadcast(intent);});}}
(3)Test
  1. 第 1 次点击按钮,发送广播,输出结果如下
收到内容 - com.my.broadcast.mybroadcast.TestReceiver@1a66a22
  1. 第 2 次点击按钮,发送广播,输出结果如下
收到内容 - com.my.broadcast.mybroadcast.TestReceiver@58d710f

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

1、基本介绍
  • 动态注册的广播接收器,每次发送广播,只有一个广播接收器实例
2、演示
(1)Receiver
publicclassTestReceiverextendsBroadcastReceiver{publicstaticfinalStringTAG=TestReceiver.class.getSimpleName();publicstaticfinalStringACTION=TAG;@OverridepublicvoidonReceive(Contextcontext,Intentintent){Log.i(TAG,"收到内容 - "+this);}}
(2)Activity
  • activity_main.xml
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns: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"><Buttonandroid: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
publicclassMainActivityextendsAppCompatActivity{publicstaticfinalStringTAG=MainActivity.class.getSimpleName();@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);TestReceiverreceiver=newTestReceiver();IntentFilterintentFilter=newIntentFilter(TestReceiver.ACTION);registerReceiver(receiver,intentFilter);ButtonbtnSend=findViewById(R.id.btn_send);btnSend.setOnClickListener(v->{Intentintent=newIntent();intent.setAction(TestReceiver.ACTION);sendBroadcast(intent);});}}
(3)Test
  1. 第 1 次点击按钮,发送广播,输出结果如下
收到内容 - com.my.broadcast.mybroadcast.TestReceiver@6458b62
  1. 第 2 次点击按钮,发送广播,输出结果如下
收到内容 - com.my.broadcast.mybroadcast.TestReceiver@6458b62

三、在 Application 中动态注册

1、基本介绍
  • 在 Application 的 onCreate 方法中采用动态注册来注册广播接收器,只会创建一个广播接收器实例
2、演示
(1)Receiver
publicclassTestReceiverextendsBroadcastReceiver{publicstaticfinalStringTAG=TestReceiver.class.getSimpleName();publicstaticfinalStringACTION=TAG;@OverridepublicvoidonReceive(Contextcontext,Intentintent){Log.i(TAG,"收到内容 - "+this);}}
(2)Application
  • MyApplication.java
publicclassMyApplicationextendsApplication{privateTestReceivertestReceiver;@OverridepublicvoidonCreate(){super.onCreate();testReceiver=newTestReceiver();IntentFilterfilter=newIntentFilter(TestReceiver.ACTION);registerReceiver(testReceiver,filter);}}
(3)Activity
  • activity_main.xml
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns: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"><Buttonandroid: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
publicclassMainActivityextendsAppCompatActivity{publicstaticfinalStringTAG=MainActivity.class.getSimpleName();@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ButtonbtnSend=findViewById(R.id.btn_send);btnSend.setOnClickListener(v->{Intentintent=newIntent();intent.setAction(TestReceiver.ACTION);sendBroadcast(intent);});}}
(4)Test
  1. 第 1 次点击按钮,发送广播,输出结果如下
收到内容 - com.my.broadcast.mybroadcast.TestReceiver@332c7bc
  1. 第 2 次点击按钮,发送广播,输出结果如下
收到内容 - com.my.broadcast.mybroadcast.TestReceiver@332c7bc

四、在 Activity 中动态注册

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

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

2、多个广播接收器实例
(1)Receiver
publicclassTestReceiverextendsBroadcastReceiver{publicstaticfinalStringTAG=TestReceiver.class.getSimpleName();publicstaticfinalStringACTION=TAG;@OverridepublicvoidonReceive(Contextcontext,Intentintent){Log.i(TAG,"收到内容 - "+this);}}
(2)Activity
  • activity_main.xml
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns: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"><Buttonandroid: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
publicclassMainActivityextendsAppCompatActivity{publicstaticfinalStringTAG=MainActivity.class.getSimpleName();@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);registerReceiver(newTestReceiver(),newIntentFilter(TestReceiver.ACTION));registerReceiver(newTestReceiver(),newIntentFilter(TestReceiver.ACTION));ButtonbtnSend=findViewById(R.id.btn_send);btnSend.setOnClickListener(v->{Intentintent=newIntent();intent.setAction(TestReceiver.ACTION);sendBroadcast(intent);});}}
(3)Test
  1. 第 1 次点击按钮,发送广播,输出结果如下
收到内容 - com.my.broadcast.mybroadcast.TestReceiver@6458b62 收到内容 - com.my.broadcast.mybroadcast.TestReceiver@be064f3
  1. 第 2 次点击按钮,发送广播,输出结果如下
收到内容 - com.my.broadcast.mybroadcast.TestReceiver@6458b62 收到内容 - com.my.broadcast.mybroadcast.TestReceiver@be064f3
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 4:35:31

基于随机森林模型的轴承剩余寿命预测MATLAB实现!

一、研究背景 轴承是旋转机械设备的核心部件&#xff0c;其健康状况直接影响设备的可靠性和安全性。传统的定期维护存在"过度维护"或"维护不足"的问题&#xff0c;预测性维护通过实时监测轴承状态并预测剩余寿命&#xff0c;可实现&#xff1a; 减少非计划…

作者头像 李华
网站建设 2026/6/10 11:07:33

SQL 性能雷区揭秘:为何阿里等大厂严禁使用 ORDER BY RAND()?

在日常开发中&#xff0c;我们常遇到“随机取几条数据”的需求&#xff0c;比如首页推荐、抽奖系统或内容轮播。许多开发者会不假思索地写出如下 SQL&#xff1a; vbnet SELECT * FROM products ORDER BY RAND() LIMIT 5; 简洁、直观、看似完美——但正是这条语句&#xff0…

作者头像 李华
网站建设 2026/6/10 11:10:24

寒假集训8——数论

P1009 [NOIP 1998 普及组] 阶乘之和题目描述用高精度计算出 S1!2!3!⋯n!&#xff08;n≤50&#xff09;。其中 ! 表示阶乘&#xff0c;定义为 n!n(n−1)(n−2)⋯1。例如&#xff0c;5!54321120。输入格式一个正整数 n。输出格式一个正整数 S&#xff0c;表示计算结果。输入输出…

作者头像 李华