1-AII--BroadcastReceiver广播的静态注册与动态注册
生活随笔
收集整理的這篇文章主要介紹了
1-AII--BroadcastReceiver广播的静态注册与动态注册
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、靜態廣播注冊
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;@Overrideprotected 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 {@Overridepublic 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"));二、動態注冊
在未注冊之前,點擊發送無效果,在注冊后點擊發送有效果,在注銷之后點擊無效果。
點擊的三個核心代碼見下。
注冊方法:
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.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"><TextViewandroid: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"/><Buttonandroid: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"/><Buttonandroid: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"/><Buttonandroid: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總結
以上是生活随笔為你收集整理的1-AII--BroadcastReceiver广播的静态注册与动态注册的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 三. 线程管理之ThreadLocal
- 下一篇: (新聞) 2008 Altera 亞洲創