使用LocalBroadcastManager

LocalBroadcastManagerAndroid Support包提供了一个工具,是用来在同一个应用内的不同组件间发送Broadcast的。

使用LocalBroadcastManager有如下好处:

  • 发送的广播只会在自己App内传播,不会泄露给其他App,确保隐私数据不会泄露
  • 其他App也无法向你的App发送该广播,不用担心其他App会来搞破坏
  • 比系统全局广播更加高效

发送广播:

final Intent intent = new Intent(UartService.DATAUPDATA);
        LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent);

接收广播:

LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
                updateReceiver, makeGattUpdateIntentFilter());

private static IntentFilter makeGattUpdateIntentFilter() {
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(UartService.DATAUPDATA);
        return intentFilter;
    }

private final BroadcastReceiver updateReceiver = new BroadcastReceiver() {

public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            ToastUtil.toast(getActivity(), action);
            
        }
    };

时间: 2024-08-29 16:33:59

使用LocalBroadcastManager的相关文章

Android 之使用LocalBroadcastManager解决BroadcastReceiver安全问题

在Android系统中,BroadcastReceiver的设计初衷就是从全局考虑的,可以方便应用程序和系统.应用程序之间.应用程序内的通信,所以对单个应用程序而言BroadcastReceiver是存在安全性问题的,相应问题及解决如下: 1.当应用程序发送某个广播时系统会将发送的Intent与系统中所有注册的BroadcastReceiver的IntentFilter进行匹配,若匹配成功则执行相应的onReceive函数.可以通过类似sendBroadcast(Intent, String)的

Android 中LocalBroadcastManager的使用方式

Android中BroadcastReceiver主要用途有 发送通知,更新UI或者数据,应用程序间相互通信,监听系统状态(比如开机,网络等) Android中BroadcasetReceiver的注册方式 manifest清单文件中的全局注册 按照生命周期,在Service或者Activity中使用代码注册 manifest的注册方式  <receiver android:name="com.sample.test.MyBroadcastReciever">       

BroadcastReceiver广播接收者(四)——本地广播LocalBroadcastManager以及在onReceive()中弹出Dialog

MainActivity如下: package cc.cv; import android.os.Bundle; import android.support.v4.content.LocalBroadcastManager; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.app.Activity; import an

LocalBroadcastManager 的使用

一.使用本地广播发送一条广播(本例为自己发送自己接收,本地广播也可以是其他应用接收)然后接收到广播时回调Receiver类中的回调方法onReceive()在此方法中自定义发出通知 代码 1 package com.qf.broadcastreceiver06; 2 3 import android.app.Activity; 4 import android.app.Notification; 5 import android.app.NotificationManager; 6 import

使用LocalBroadcastManager解决BroadcastReceiver安全问题

在Android系统中,BroadcastReceiver的设计初衷就是从全局考虑的,可以方便应用程序和系统.应用程序之间.应用程序内的通信,所以对单个应用程序而言BroadcastReceiver是存在安全性问题的,相应问题及解决如下: 1.当应用程序发送某个广播时系统会将发送的Intent与系统中所有注册的BroadcastReceiver的IntentFilter进行匹配,若匹配成功则执行相应的onReceive函数.可以通过类似sendBroadcast(Intent, String)的

Fragment使用LocalBroadcastManager接收广播消息

这种方式不用在配置文件加东西 变量声明 [java] view plain copy LocalBroadcastManager broadcastManager; IntentFilter intentFilter; BroadcastReceiver mReceiver; 广播注册,可以写在Activity(onCreate),也可以写在Fragment(onActivityCreated)里. [java] view plain copy broadcastManager = LocalB

LocalBroadcastManager 的实现原理,Handler还是 Binder?

原文: http://www.trinea.cn/android/localbroadcastmanager-impl/ 对 LocalBroadcastManager 大家应该都不陌生,相对 BroadcastReceiver,它只能用于应用内通信,安全性更好,同时拥有更高的运行效率.也是需要发送应用内广播时的官方推荐. 大家也都知道BroadcastReceiver的通信是走 Binder 机制的,而 LocalBroadcastManager 因为叫LocalBroadcast,可能让人产

(转)Android 中LocalBroadcastManager的使用方式

发表于2个月前(2014-11-03 22:05)   阅读(37) | 评论(0) 0人收藏此文章, 我要收藏 赞0 1月10日 #长沙# OSC 源创会第32期开始报名 摘要 android中广播的作用非常大,对程序的运行起着非常重要的作用 LocalBroadcastManager Android中BroadcastReceiver主要用途有 发送通知,更新UI或者数据,应用程序间相互通信,监听系统状态(比如开机,网络等) Android中BroadcasetReceiver的注册方式 m

LocalBroadcastManager源码分析

源码分析 /* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * htt

揭秘LocalBroadcastManager实现原理

LocalBroadcastManager经常用的App内通信组件,也是官方推荐的App内广播发送组件 起初,用这个组件的时候,见名知意,因为LocalBroadcast,所以以为也是用Binder实现的底层,结果不是这样的. 1.平时我们都是这样用LocalBroadcastManager a.注册并接收 LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(context); IntentFilt