news 2026/4/2 6:22:47

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/4/1 3:40:47

高校科研成果转化的新生态:从困局到共赢

在科技飞速发展的今天&#xff0c;高校作为科技创新的重要发源地&#xff0c;其科研成果的转化效率直接影响着国家整体创新能力的提升。然而&#xff0c;在传统转化模式下&#xff0c;高校科研处往往面临着信息壁垒、供需不对称等痛点问题。如何破解这一难题&#xff1f;“高校…

作者头像 李华
网站建设 2026/3/28 23:19:38

有效运用长尾关键词优化SEO策略的实用指南

本文将围绕长尾关键词如何优化SEO策略展开&#xff0c;深入探讨其在提升网站流量和搜索排名中的重要性。长尾关键词较为具体&#xff0c;通常由三个或更多词组成&#xff0c;能更精准地满足用户需求。在接下来的讨论中&#xff0c;我们将分析如何有效选择和运用长尾关键词&…

作者头像 李华
网站建设 2026/3/25 7:43:30

一文读懂 Java 主流编译器:特性、场景与选择指南

Java 主流编译器 一文读懂&#xff1a;特性、场景与选择指南&#xff08;2025-2026 视角&#xff09; Java 程序的“编译”分为两个阶段&#xff1a; 前端编译&#xff08;javac / ecj 等&#xff09;&#xff1a;.java → .class 字节码&#xff08;静态编译&#xff09;后端…

作者头像 李华
网站建设 2026/4/1 19:28:41

实战案例:51单片机低功耗场景下的简易滤波实现

作为嵌入式工程师或电子信息专业学习者&#xff0c;你大概率遇到过这样的实操困境&#xff1a;用51单片机开发低功耗项目&#xff08;如电池供电的温湿度采集、人体感应模块&#xff09;&#xff0c;硬件接线无误&#xff0c;但传感器采集的数据始终飘忽不定——温度忽高忽低、…

作者头像 李华