news 2026/3/2 13:09:37

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/2/27 7:27:03

百考通AI:权威AIGC检测,为学术诚信保驾护航

在人工智能技术迅猛发展的今天&#xff0c;AI写作工具已成为众多学生和研究者的重要辅助手段。然而&#xff0c;随着高校和学术机构对AI生成内容的监管日益严格&#xff0c;如何确保学术成果的原创性与真实性&#xff0c;已成为每一位学子必须面对的挑战。无论是毕业论文、学术…

作者头像 李华
网站建设 2026/3/1 0:26:43

高校科研转化新路径:生态协同机制助力学术产业双赢

在科技创新驱动发展的时代&#xff0c;高校作为知识与技术的策源地&#xff0c;其科技成果向产业转化的能力直接关系到区域经济的发展潜力。然而&#xff0c;传统科技成果转化模式中&#xff0c;信息壁垒、供需不对称以及复杂的技术经纪服务流程等问题&#xff0c;成为阻碍高校…

作者头像 李华
网站建设 2026/3/1 23:33:34

构建区域创新生态,科技成果转化的破局之道

在当今全球科技创新迅猛发展的时代背景下&#xff0c;我国各地正加速布局区域创新体系。然而&#xff0c;在这一过程中&#xff0c;科技成果转化却遭遇瓶颈问题。无论是科技成果供需信息的不对称&#xff0c;还是转化渠道不畅通&#xff0c;以及专业化服务能力不足等痛点&#…

作者头像 李华
网站建设 2026/3/2 11:10:12

Docker-in-Docker的安全风险与替代方案

Docker-in-Docker&#xff1a;安全风险深度剖析与现代化替代方案&#xff08;Sysbox&#xff09;指南 第一部分&#xff1a;开篇明义 —— 定义、价值与目标 定位与价值 在现代化的软件交付流水线&#xff08;CI/CD&#xff09;与复杂多租户开发环境中&#xff0c;Docker-in-Do…

作者头像 李华
网站建设 2026/3/2 9:39:56

AI音乐版权检测系统的技术架构与测试挑战

随着AI生成音乐在流媒体平台占比激增至39%&#xff08;2025年数据&#xff09;&#xff0c;基于Transformer的版权侵权检测系统成为行业刚需。此类系统通过分析音频信号的微观特征&#xff08;如谐波结构、时域异常&#xff09;识别AI生成内容&#xff0c;其测试需覆盖三大核心…

作者头像 李华
网站建设 2026/2/27 18:19:11

LLOOGG.com的网站的使用方法,有没有可以使用的服务,以及接口_blog

LLOOGG.com 极简实时监控&#xff1a;中国开发者使用指北 引言 在网站分析与运维领域&#xff0c;实时性往往是洞察先机的关键。对于广大中国开发者&#xff0c;特别是个人博主与初创团队而言&#xff0c;Google Analytics的复杂与延迟、百度统计的侧重长期趋势&#xff0c;有…

作者头像 李华