美文网首页Android技术栈Android技术知识Android开发
1-AII--BroadcastReceiver广播的静态注册与

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

作者: e4e52c116681 | 来源:发表于2018-08-24 19:13 被阅读2次

    一、静态广播注册

    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-AII--BroadcastReceiver广播的静态注册与

        本文链接:https://www.haomeiwen.com/subject/hauiiftx.html