zoukankan      html  css  js  c++  java
  • 使用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);
                
            }
        };

  • 相关阅读:
    CSS基础
    数据库优化之SQL Server
    压力测试与系统调优
    JBoss架构分析
    JBoss基本配置
    深入了解硬盘结构
    EJB2与EJB3架构对比
    JBoss高级配置
    病毒分类及病毒命名规则详解
    深入讲解防火墙的概念原理与实现
  • 原文地址:https://www.cnblogs.com/zhaoleigege/p/5497361.html
Copyright © 2011-2022 走看看