Android LocalBroadcastManager提高应用安全性

本文介绍了Android中本地广播管理器(LocalBroadcastManager)的功能与优势,包括保护应用内部数据、提高安全性及提升效率等方面,并提供了简单的注册与发送广播的示例代码。

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

  • 官方文档解释:Helper to register for and send broadcasts of Intents to local objects within your process. This is has a number of advantages over sending global broadcasts with Context.sendBroadcast(android.content.Intent):
    • You know that the data you are broadcasting 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 efficient than sending a global broadcast through the system.
    意义是:翻译下,1、你正在发送的数据不会超出你的应用,所以不用担心泄露应用私有数据。

  • 2、其他应用发送的广播不会发送到你的APP,所以你不用担心有安全性的攻击。
  • 3、本地广播比全局广播更高效
使用方法:非常简单,该类是采用单例,

创建

    final LocalBroadcastManager localBroadcastManager=LocalBroadcastManager.getInstance(this);

注册:


    IntentFilter filter=new IntentFilter();
    filter.addAction("duanqing.test.localbroadcast.fly");
    localBroadcastManager.registerReceiver(new MyBroadCastReceiver(),filter);


发送代码:

    Intent intent=new Intent();
    intent.setAction("duanqing.test.localbroadcast.fly");
    localBroadcastManager.sendBroadcast(intent);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值