Android中如何使用本地广播

Android中如何使用本地广播

这篇文章主要介绍了Android中如何使用本地广播的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Android中如何使用本地广播文章都会有所收获,下面我们一起来看看吧。

MainActivity代码

Android中如何使用本地广播

packagecom.example.luobo.mybroadcastreceiver;importandroid.content.BroadcastReceiver;importandroid.content.Context;importandroid.content.Intent;importandroid.content.IntentFilter;importandroid.support.v4.content.LocalBroadcastManager;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.Toast;publicclassMainActivityextendsAppCompatActivityimplementsView.OnClickListener{privateButtonbutton;privateIntentFilterintentFilter;privateLocalBroadcastManagerlocalBroadcastManager;privateLocalReceiverlocalReciiver;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button=(Button)findViewById(R.id.send_button);button.setOnClickListener(this);localBroadcastManager=LocalBroadcastManager.getInstance(this);//使用intentFilter=newIntentFilter();intentFilter.addAction("com.example.luobo.mybroadcastreceiver.LOCAL_BROADCAST");localReciiver=newLocalReceiver();localBroadcastManager.registerReceiver(localReciiver,intentFilter);}@OverrideprotectedvoidonDestroy(){super.onDestroy();localBroadcastManager.unregisterReceiver(localReciiver);}@OverridepublicvoidonClick(Viewview){Intentintent=newIntent("com.example.luobo.mybroadcastreceiver.LOCAL_BROADCAST");localBroadcastManager.sendBroadcast(intent);}classLocalReceiverextendsBroadcastReceiver{@OverridepublicvoidonReceive(Contextcontext,Intentintent){Toast.makeText(context,"receivedlocalbroadcast",Toast.LENGTH_SHORT).show();}}}

首先通过LocalBroadcastManager(本地广播管理类)的getInstance(this)方法获取实例,注册广播消息时是调用localBroadcastManager实例的registerReceiver(参数1,参数2)方法注册(参数1是本地广播接受者,参数2是过滤器只选择接收特定的广播消息),调用localBroadcastManager实例的sendBroadcast(Initent initent)方法发送广播消息。

MyRecevity

packagecom.example.luobo.mybroadcastreceiver;importandroid.content.BroadcastReceiver;importandroid.content.Context;importandroid.content.Intent;importandroid.widget.Toast;publicclassMyReceiverextendsBroadcastReceiver{@OverridepublicvoidonReceive(Contextcontext,Intentintent){Toast.makeText(context,"ReceivedinMyBroadCastReceiver",Toast.LENGTH_SHORT).show();abortBroadcast();}}

activity_main.xml

<?xmlversion="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="com.example.luobo.mybroadcastreceiver.MainActivity"><Buttonandroid:id="@+id/send_button"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="发送广播"/></android.support.constraint.ConstraintLayout>

AndroidMainfest.aml

<?xmlversion="1.0"encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.example.luobo.mybroadcastreceiver"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><activityandroid:name=".MainActivity"><intent-filter><actionandroid:name="android.intent.action.MAIN"/><categoryandroid:name="android.intent.category.LAUNCHER"/></intent-filter></activity><receiverandroid:name=".MyReceiver"android:enabled="true"android:exported="true"><intent-filterandroid:priority="100"><actionandroid:name="com.example.luobo.mybroadcastreceiver.LOCAL_BROADCAST"/></intent-filter></receiver></application></manifest>

关于“Android中如何使用本地广播”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Android中如何使用本地广播”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注恰卡编程网行业资讯频道。

发布于 2022-04-15 22:29:19
收藏
分享
海报
0 条评论
25
上一篇:Android中如何实现图片缓存机制 下一篇:Android中如何实现短信编辑器功能
目录

    推荐阅读

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码