前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >1-AII--BroadcastReceiver广播的静态注册与动态注册

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

作者头像
张风捷特烈
发布2018-09-26 16:56:59
9590
发布2018-09-26 16:56:59
举报
一、静态广播注册
MainActivity.java
代码语言:javascript
复制
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
代码语言:javascript
复制
/**
 * 作者:张风捷特烈
 * 时间: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
代码语言:javascript
复制
<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(包全名,类全名))
代码语言:javascript
复制
intent.setComponent(new ComponentName("com.toly1994.aii_broadcastreceiver",
                        "com.toly1994.aii_broadcastreceiver.StaticBR"));

二、动态注册

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

动态注册广播.gif

注册方法:
代码语言:javascript
复制
IntentFilter filter = new IntentFilter();
filter.addAction("com.toly1994.aii_broadcastreceiver.register");
mReceiver = new StaticBR();
registerReceiver(mReceiver, filter);
发送方法:
代码语言:javascript
复制
Intent intent = new Intent();
//注意setAction与AndroidManifest中的action对应
intent.setAction("com.toly1994.aii_broadcastreceiver.register");
intent.putExtra("msg", "张风捷特烈");
sendBroadcast(intent);
注销方法:
代码语言:javascript
复制
if (mReceiver != null) {
    unregisterReceiver(mReceiver);
    mReceiver = null;
}
附录:布局文件:activity_main.xml
代码语言:javascript
复制
<?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>

本文由张风捷特烈原创,转载请注明 更多安卓技术欢迎访问:https://www.jianshu.com/c/004f3fe34c94 张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com 你的喜欢与支持将是我最大的动力

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.08.24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、静态广播注册
    • MainActivity.java
      • 静态注册广播接受者:StaticBR.java
        • 静态注册:app/src/main/AndroidManifest.xml
          • Android8.0静态广播解决方案:intent.setComponent(new ComponentName(包全名,类全名))
          • 二、动态注册
            • 注册方法:
              • 发送方法:
                • 注销方法:
                  • 附录:布局文件:activity_main.xml
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档