LocalBoardcast 与 Boradcast

本来是在实验通过广播来获取系统状态的功能的. 偶然间发现还有LocalBoardcast这个东西 -_- 真汗颜.
赶紧恶补一下..

摘自网络博客:
在android-support-v4.jar中引入了LocalBroadcastManager,称为局部通知管理器,这种通知的好处是安全性高,效率也高,适合局部通信,可以用来代替Handler更新UI

现在总结一下主要使用流程.(相当简单)
用代码表示了.
(1) 设定一个广播

private ConnectivityManager mConnectivityManager = null;
private NetworkInfo mNetworkInfo = null;

private BroadcastReceiver mNetworkBoardcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent == null) {
            return;
        }
//            Log.d(logTitle, "onReceive: some boardcast" + intent.toString());
        String action = intent.getAction();
        if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            mConnectivityManager = (ConnectivityManager) getSystemService(Context
                    .CONNECTIVITY_SERVICE);
            mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
                if (mNetworkInfo.getTypeName().equals("WIFI")) {
                    sendNetworkStateBoardcast(ACTION_CONNECTIONED);
                    Log.d(logTitle, "netstate to:" + mNetworkInfo.getTypeName());

                } else {
                    sendNetworkStateBoardcast(STATE_NETWORK_MOBILE);
                    Log.d(logTitle, "netstate to:" + mNetworkInfo.getTypeName());
                }

            } else {
                sendNetworkStateBoardcast(ACTION_NO_CONNECTION);
            }
        }
    }
};

private void sendNetworkStateBoardcast(String state) {
    Intent sendIntent = new Intent(state);  
    LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(sendIntent);
}

(2) 设定广播接收器

public class BoardcastReceiverDetectSystemStatus extends BroadcastReceiver {
    Context currentContext;
    public BoardcastReceiverDetectSystemStatus() {
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("broadreveiver", "onReceive: intent is" + intent.toString());
    }
}

(3) activity内设定本地广播接收

        boardcastReceiverDetectSystemStatus = new BoardcastReceiverDetectSystemStatus(this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ServiceDetectSystemStatus.ACTION_CONNECTIONED);
        intentFilter.addAction(ServiceDetectSystemStatus.ACTION_NO_CONNECTION);
        LocalBroadcastManager.getInstance(this).registerReceiver(boardcastReceiverDetectSystemStatus, intentFilter);

以上就是三个主要步骤, 当然, 后期可以根据需要进行二次封装以及改造. 去耦

官方文档:
https://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,288评论 25 709
  • 本文转载自http://www.cnblogs.com/lwbqqyumidi/p/4168017.html 1....
    Ernest_Chang阅读 731评论 0 3
  • 诸多无奈,最近在改一个项目的bug的时候,脑子老是一片空白,可能是最近脑子不够用,总之,好多东西忘了,阅读一些别人...
    狗子王1948阅读 7,772评论 6 53
  • 每一棵玉米 都是一位抱着婴儿的母亲 我的每一条走出村庄的路 都被密密的玉米林遮住淹没了 有一次我硬闯进去 走了几天...
    燕赵大侠阅读 367评论 2 0
  • 今天天气不错
    龚旭阅读 118评论 0 0