美文网首页
LocalBoardcast 与 Boradcast

LocalBoardcast 与 Boradcast

作者: 冰风47 | 来源:发表于2016-06-03 16:53 被阅读247次

    本来是在实验通过广播来获取系统状态的功能的. 偶然间发现还有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

    相关文章

      网友评论

          本文标题:LocalBoardcast 与 Boradcast

          本文链接:https://www.haomeiwen.com/subject/vafldttx.html