1-AII--BroadcastReceiver广播的静态注册与动态注册

mac2022-06-30  81

一、静态广播注册

MainActivity.java
public class MainActivity extends AppCompatActivity { @BindView(R.id.btn_send) Button mBtnSend; @BindView(R.id.btn_unregister) Button mBtnUnregister; @BindView(R.id.btn_register) Button mBtnRegister; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); } @OnClick({R.id.btn_send}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.btn_send: Intent intent = new Intent(); //注意setAction与AndroidManifest中的action对应 intent.setAction("com.toly1994.aii_broadcastreceiver.StaticBR"); intent.putExtra("msg" , "张风捷特烈"); sendBroadcast(intent); break; } } }
静态注册广播接受者:StaticBR.java
/** * 作者:张风捷特烈 * 时间:2018/4/14:16:22 * 邮箱:1981462002@qq.com * 说明:静态注册广播接受者 */ public class StaticBR extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String msg = intent.getStringExtra("msg"); ToastUtil.show(context, msg + "\n第一个简单广播创建成功!"); } }
静态注册:app/src/main/AndroidManifest.xml
<receiver android:name=".StaticBR"> <intent-filter> <action android:name="com.toly1994.aii_broadcastreceiver.MyBroadcastReceiver"/> </intent-filter> </receiver>

经测试,Android8.0无法收到静态广播,Android7.0可以法收到静态广播 静态注册一大好处是可以跨程序使用,A程序中的BroadcastReceiver可以在B程序中使用

Android8.0静态广播解决方案:intent.setComponent(new ComponentName(包全名,类全名))
intent.setComponent(new ComponentName("com.toly1994.aii_broadcastreceiver", "com.toly1994.aii_broadcastreceiver.StaticBR"));

二、动态注册

在未注册之前,点击发送无效果,在注册后点击发送有效果,在注销之后点击无效果。 点击的三个核心代码见下。

动态注册广播.gif
注册方法:
IntentFilter filter = new IntentFilter(); filter.addAction("com.toly1994.aii_broadcastreceiver.register"); mReceiver = new StaticBR(); registerReceiver(mReceiver, filter);
发送方法:
Intent intent = new Intent(); //注意setAction与AndroidManifest中的action对应 intent.setAction("com.toly1994.aii_broadcastreceiver.register"); intent.putExtra("msg", "张风捷特烈"); sendBroadcast(intent);
注销方法:
if (mReceiver != null) { unregisterReceiver(mReceiver); mReceiver = null; }
附录:布局文件:activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"/> <Button android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:layout_marginStart="16dp" android:layout_marginTop="16dp" android:text="发送广播" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/> <Button android:id="@+id/btn_unregister" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" android:layout_marginTop="16dp" android:text="注销广播" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent"/> <Button android:id="@+id/btn_register" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="注册广播" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/> </android.support.constraint.ConstraintLayout>

后记、

1.声明:

[1]本文由张风捷特烈原创,转载请注明 [2]欢迎广大编程爱好者共同交流 [3]个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正 [4]你的喜欢与支持将是我最大的动力

2.连接传送门:

更多安卓技术欢迎访问:安卓技术栈 我的github地址:欢迎star 简书首发,腾讯云+社区同步更新张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com

3.联系我

QQ:1981462002 邮箱:1981462002@qq.com 微信:zdl1994328

4.欢迎关注我的微信公众号,最新精彩文章,及时送达:
公众号.jpg

转载于:https://www.cnblogs.com/toly-top/p/9781949.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)