日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > ChatGpt >内容正文

ChatGpt

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

發布時間:2025/3/17 ChatGpt 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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"));

二、動態注冊

在未注冊之前,點擊發送無效果,在注冊后點擊發送有效果,在注銷之后點擊無效果。
點擊的三個核心代碼見下。

動態注冊廣播.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.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广播的静态注册与动态注册的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。