本地广播信息只能在应用程序内部传递,同时广播接收器也只能接收应用程序内部的广播消息。

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中BroadcastReceiver(异步接收广播Intent)的使用
# Android 广播大全 Intent Action 事件详解
# Android apk安装替换卸载广播的实现代码
# Android中的广播和广播接收器代码实例
# android连接wifi时获取广播地址代码
# Android基于广播事件机制实现简单定时提醒功能代码
# Android广播接收机制详细介绍(附短信接收实现)
# Android广播接实现监听电话状态(电话的状态
# 拦截)
# Android 开机广播的使用及配置
# android如何默认打开小区广播具体实现
# 应用程序
# 大家多多
# 管理类
# 只能在
# void
# protected
# Override
# onCreate
# setContentView
# super
# savedInstanceState
# localReciiver
# private
# OnClickListener
# implements
# button
# LocalReceiver
# localBroadcastManager
# intentFilter
# layout
相关文章:
岳西云建站教程与模板下载_一站式快速建站系统操作指南
建站之星代理平台如何选择最佳方案?
如何通过FTP空间快速搭建安全高效网站?
宝塔建站助手安装配置与建站模板使用全流程解析
建站主机是否等同于虚拟主机?
*服务器网站为何频现安全漏洞?
如何制作公司的网站链接,公司想做一个网站,一般需要花多少钱?
建站之星导航如何优化提升用户体验?
微信网站制作公司有哪些,民生银行办理公司开户怎么在微信网页上查询进度?
如何快速生成橙子建站落地页链接?
桂林网站制作公司有哪些,桂林马拉松怎么报名?
如何通过智能用户系统一键生成高效建站方案?
如何获取上海专业网站定制建站电话?
外贸公司网站制作哪家好,maersk船公司官网?
企业网站制作公司网页,推荐几家专业的天津网站制作公司?
网站制作公司,橙子建站是合法的吗?
网站网页制作电话怎么打,怎样安装和使用钉钉软件免费打电话?
在线ppt制作网站有哪些,请推荐几个好的课件下载的网站?
表情包在线制作网站免费,表情包怎么弄?
制作网站公司那家好,网络公司是做什么的?
ppt制作免费网站有哪些,ppt模板免费下载网站?
购物网站制作费用多少,开办网上购物网站,需要办理哪些手续?
建站之星ASP如何实现CMS高效搭建与安全管理?
官网自助建站平台指南:在线制作、快速建站与模板选择全解析
音乐网站服务器如何优化API响应速度?
如何在宝塔面板中修改默认建站目录?
C++中引用和指针有什么区别?(代码说明)
建站主机SSH密钥生成步骤及常见问题解答?
兔展官网 在线制作,怎样制作微信请帖?
如何高效完成自助建站业务培训?
矢量图网站制作软件,用千图网的一张矢量图做公司app首页,该网站并未说明版权等问题,这样做算不算侵权?应该如何解决?
如何在建站宝盒中设置产品搜索功能?
如何高效生成建站之星成品网站源码?
微课制作网站有哪些,微课网怎么进?
常州自助建站费用包含哪些项目?
内网网站制作软件,内网的网站如何发布到外网?
javascript中对象的定义、使用以及对象和原型链操作小结
如何制作新型网站程序文件,新型止水鱼鳞网要拆除吗?
南京网站制作费用,南京远驱官方网站?
油猴 教程,油猴搜脚本为什么会网页无法显示?
如何优化Golang Web性能_Golang HTTP服务器性能提升方法
实惠建站价格推荐:2025年高性价比自助建站套餐解析
php能控制zigbee模块吗_php通过串口与cc2530 zigbee通信【介绍】
建站之星如何一键生成手机站?
网站制作多少钱一个,建一个论坛网站大约需要多少钱?
成都响应式网站开发,dw怎么把手机适应页面变成网页?
全景视频制作网站有哪些,全景图怎么做成网页?
如何在Windows服务器上快速搭建网站?
建站之星如何实现五合一智能建站与营销推广?
宝塔面板创建网站无法访问?如何快速排查修复?
*请认真填写需求信息,我们会在24小时内与您取得联系。