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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人工智能 > ChatGpt >内容正文

ChatGpt

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

發(fā)布時(shí)間:2025/3/17 ChatGpt 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 1-AII--BroadcastReceiver广播的静态注册与动态注册 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、靜態(tài)廣播注冊(cè)

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對(duì)應(yīng)intent.setAction("com.toly1994.aii_broadcastreceiver.StaticBR");intent.putExtra("msg" , "張風(fēng)捷特烈");sendBroadcast(intent);break;}} }
靜態(tài)注冊(cè)廣播接受者:StaticBR.java
/*** 作者:張風(fēng)捷特烈* 時(shí)間:2018/4/14:16:22* 郵箱:1981462002@qq.com* 說(shuō)明:靜態(tài)注冊(cè)廣播接受者*/ public class StaticBR extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {String msg = intent.getStringExtra("msg");ToastUtil.show(context, msg + "\n第一個(gè)簡(jiǎn)單廣播創(chuàng)建成功!");} }
靜態(tài)注冊(cè):app/src/main/AndroidManifest.xml
<receiver android:name=".StaticBR"><intent-filter><action android:name="com.toly1994.aii_broadcastreceiver.MyBroadcastReceiver"/></intent-filter></receiver>

經(jīng)測(cè)試,Android8.0無(wú)法收到靜態(tài)廣播,Android7.0可以法收到靜態(tài)廣播
靜態(tài)注冊(cè)一大好處是可以跨程序使用,A程序中的BroadcastReceiver可以在B程序中使用

Android8.0靜態(tài)廣播解決方案:intent.setComponent(new ComponentName(包全名,類全名))
intent.setComponent(new ComponentName("com.toly1994.aii_broadcastreceiver","com.toly1994.aii_broadcastreceiver.StaticBR"));

二、動(dòng)態(tài)注冊(cè)

在未注冊(cè)之前,點(diǎn)擊發(fā)送無(wú)效果,在注冊(cè)后點(diǎn)擊發(fā)送有效果,在注銷之后點(diǎn)擊無(wú)效果。
點(diǎn)擊的三個(gè)核心代碼見(jiàn)下。

動(dòng)態(tài)注冊(cè)廣播.gif
注冊(cè)方法:
IntentFilter filter = new IntentFilter(); filter.addAction("com.toly1994.aii_broadcastreceiver.register"); mReceiver = new StaticBR(); registerReceiver(mReceiver, filter);
發(fā)送方法:
Intent intent = new Intent(); //注意setAction與AndroidManifest中的action對(duì)應(yīng) intent.setAction("com.toly1994.aii_broadcastreceiver.register"); intent.putExtra("msg", "張風(fēng)捷特烈"); 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="發(fā)送廣播"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="注冊(cè)廣播"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"/></android.support.constraint.ConstraintLayout>

后記、

1.聲明:

[1]本文由張風(fēng)捷特烈原創(chuàng),轉(zhuǎn)載請(qǐng)注明
[2]歡迎廣大編程愛(ài)好者共同交流
[3]個(gè)人能力有限,如有不正之處歡迎大家批評(píng)指證,必定虛心改正
[4]你的喜歡與支持將是我最大的動(dòng)力

2.連接傳送門:

更多安卓技術(shù)歡迎訪問(wèn):安卓技術(shù)棧
我的github地址:歡迎star
簡(jiǎn)書(shū)首發(fā),騰訊云+社區(qū)同步更新
張風(fēng)捷特烈個(gè)人網(wǎng)站,編程筆記請(qǐng)?jiān)L問(wèn):http://www.toly1994.com

3.聯(lián)系我

QQ:1981462002
郵箱:1981462002@qq.com
微信:zdl1994328

4.歡迎關(guān)注我的微信公眾號(hào),最新精彩文章,及時(shí)送達(dá):
公眾號(hào).jpg

總結(jié)

以上是生活随笔為你收集整理的1-AII--BroadcastReceiver广播的静态注册与动态注册的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。