LocalBroadcastManager使用简析

本文详细解释了Android应用内部广播通信类LocalBroadcastManager的使用方式及其相较于全局广播的优点,包括数据安全性、效率提升及内部方法的便利性。提供注册、发送和取消接收广播的具体代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LocalBroadcastManager属于v4包里面的类。

android.support.v4.content.LocalBroadcastManager

developer里面OverView是这么写的:
Helper to register for and send broadcasts of intents to local objects within your peocess.this is has a number of advantages over sending global broadcasts with sendBroadcast(Intent):
.You know that the data you are braodcasting won’t leave your app,so don’t need to worry about leaking private data.
.It is not possible for other applications to send these broadcasts to your app,so you don’t need to worry about having security holes they can exploit.
.It is more effcient than sending a global broadcast through the system.
通过粗体部分能看出来这是一个在你app内部通信的方法,在app内部通信上和全局的boradcast相比有优势。体现为:
1.因为是内部通信不会离开该app,所以不用担心数据泄露出去。
2.比全局boradcast更有效率.
3.内部方法不用担心安全问题.
使用也很简单:

LocalBroadCastManager.getInstance(context).registerReceiver(receiver,intentfilter);
LocalBroadCastManager.getInstance(context).sendBroadcast(Intent);
LocalBroadCastManager.getInstance(context).unregisterReiver(receiver);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值