亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

1-AII--BroadcastReceiver廣播的靜態注冊與動態注冊

標簽:
JavaScript

一、静态广播注册

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
 * 邮箱:[email protected]
 * 说明:静态注册广播接受者
 */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>



作者:张风捷特烈
链接:https://www.jianshu.com/p/68695b1e026c


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消