
本地广播信息只能在应用程序内部传递,同时广播接收器也只能接收应用程序内部的广播消息。
MainActivity代码
package com.example.luobo.mybroadcastreceiver;import androID.content.broadcastReceiver;import androID.content.Context;import androID.content.Intent;import androID.content.IntentFilter;import androID.support.v4.content.LocalbroadcastManager;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.Toast;public class MainActivity extends AppCompatActivity implements VIEw.OnClickListener{ private button button; private IntentFilter intentFilter; private LocalbroadcastManager localbroadcastManager ; private LocalReceiver localReciiver; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); button = (button)findVIEwByID(R.ID.send_button); button.setonClickListener(this); localbroadcastManager = LocalbroadcastManager.getInstance(this);//使用 intentFilter = new IntentFilter(); intentFilter.addAction("com.example.luobo.mybroadcastreceiver.LOCAL_broADCAST"); localReciiver = new LocalReceiver(); localbroadcastManager.registerReceiver(localReciiver,intentFilter); } @OverrIDe protected voID onDestroy() { super.onDestroy(); localbroadcastManager.unregisterReceiver(localReciiver); } @OverrIDe public voID onClick(VIEw vIEw) { Intent intent = new Intent("com.example.luobo.mybroadcastreceiver.LOCAL_broADCAST"); localbroadcastManager.sendbroadcast(intent); } class LocalReceiver extends broadcastReceiver{ @OverrIDe public voID onReceive(Context context,Intent intent) { Toast.makeText(context,"received local broadcast",Toast.LENGTH_SHORT).show(); } }}首先通过LocalbroadcastManager(本地广播管理类)的getInstance(this)方法获取实例,注册广播消息时是调用localbroadcastManager实例的registerReceiver(参数1,参数2)方法注册(参数1是本地广播接受者,参数2是过滤器只选择接收特定的广播消息),调用localbroadcastManager实例的sendbroadcast(Initent initent)方法发送广播消息。
MyRecevity
package com.example.luobo.mybroadcastreceiver;import androID.content.broadcastReceiver;import androID.content.Context;import androID.content.Intent;import androID.Widget.Toast;public class MyReceiver extends broadcastReceiver { @OverrIDe public voID onReceive(Context context,Intent intent) { Toast.makeText(context,"Received in MybroadCastReceiver",Toast.LENGTH_SHORT).show(); abortbroadcast(); }}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="com.example.luobo.mybroadcastreceiver.MainActivity"> <button androID:ID="@+ID/send_button" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="发送广播"/></androID.support.constraint.ConstraintLayout>
AndroIDMainfest.aml
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="com.example.luobo.mybroadcastreceiver"> <application androID: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"> <activity androID:name=".MainActivity"> <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver androID:name=".MyReceiver" androID:enabled="true" androID:exported="true"> <intent-filter androID:priority="100"> <action androID:name="com.example.luobo.mybroadcastreceiver.LOCAL_broADCAST"/> </intent-filter> </receiver> </application></manifest>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android学习之本地广播使用方法详解全部内容,希望文章能够帮你解决Android学习之本地广播使用方法详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)