美文网首页
LocalBroadcast

LocalBroadcast

作者: 小声音大世界 | 来源:发表于2018-07-05 16:22 被阅读0次

场景:在设备中需要对网络状态进行监听,当网络状态改变时,要发送指令到单片机程序中
第一步,定义推送广播的PushAction

public class PushAction {
/**
 * 包名
 */
private static final String PACKAGE_NAME = "com.xxxx.xxx";
/**
 * wifi连接成功
 */
public static final String ACTION_WIFI_CONNECT = PACKAGE_NAME + ".ACTION_WIFI_CONNECT";
/**
 * wifi连接失败
 */
public static final String ACTION_NETWORK_DISCONNECT = PACKAGE_NAME + ".ACTION_WIFI_DISCONNECT";
/**
 * 有线网络连接
 */
public static final String ACTION_ENTHERNET_CONNECT = PACKAGE_NAME + ".ACTION_ENTHERNET_CONNECT";
/**
 * 通知设备数据更新
 */
public static final String  ACTION_SCHOOLINFO_UPDATE = PACKAGE_NAME + ".ACTION_SCHOOLINFO_UPDATE";
/**
 * 设备未绑定
 */
public static final String ACTION_DEVICE_UNBIND = PACKAGE_NAME + ".ACTION_DEVICE_UNBIND";
/**
 * 时间更新后刷新错误的刷卡记录
 */
public static final String ACTION_UPLOAD_REFRESH = PACKAGE_NAME + ".ACTION_UPLOAD_REFRESH";
/**
 * MCU升级广播
 */
public static final String APP_MSG_TEST = "APP_MSG_TEST";
}

第二步,在Mainactivity中添加监听

 private void InitLocalBroadcast(){
    mLocalActionBroadcastReceiver = new LocalActionBroadcastReceiver();
    IntentFilter filter = new IntentFilter();
    filter.addAction(PushAction.ACTION_WIFI_CONNECT);
    filter.addAction(PushAction.ACTION_NETWORK_DISCONNECT);
    filter.addAction(PushAction.ACTION_ENTHERNET_CONNECT);
    filter.addAction(PushAction.ACTION_SCHOOLINFO_UPDATE);
    filter.addAction(PushAction.ACTION_DEVICE_UNBIND);
    filter.addAction(PushAction.ACTION_UPLOAD_REFRESH);
    filter.addAction(PushAction.APP_MSG_TEST);
    LocalBroadcastManager.getInstance(this)
            .registerReceiver(mLocalActionBroadcastReceiver, filter);

    registerReceiver(mLocalActionBroadcastReceiver, filter);
}

private class LocalActionBroadcastReceiver extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        switch (action) {
            case PushAction.ACTION_WIFI_CONNECT:
                mDeviceMcuSend.SetWifi();


                break;
            case PushAction.ACTION_NETWORK_DISCONNECT:
                mDeviceMcuSend.SetWifiDisable();
                break;
            case PushAction.ACTION_ENTHERNET_CONNECT:
                mDeviceMcuSend.SetEnthernet();
                break;
            case PushAction.ACTION_SCHOOLINFO_UPDATE:
                mSchooldataPresenter.ReFreshSchoolData();
                break;
            case PushAction.ACTION_DEVICE_UNBIND:
                mDeviceMcuSend.SetSchoolName("设备未绑定");
                break;
            case PushAction.ACTION_UPLOAD_REFRESH:
                Log.d("kuangtingchun","refresh");
                mRecordUploader.RereshRecord();
                break;
            case PushAction.APP_MSG_TEST:
                Log.d("DeviceMcuReceive","McuReceive");
                Intent Myintent =new Intent();

                Myintent.setAction("UPDATE_MSG_TEST");
                Myintent.putExtra("UPDATE_MSG_TEST",1000);
                sendBroadcast(Myintent);
                mSerialManager.destroy();
                System.exit(0);
                break;
        }

    }
}

第三步:啥时候发送广播呢,当网络状态改变时

public class NetworkStateChange extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    if(intent.getAction().equals("android.net.conn.CONNECTIVITY_CHANGE")){
    State wifiState = null;  
  State mobileState = null;
  State ETHERNET = null;
  ConnectivityManager cm = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);  
  wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();  
  mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        Intent mIntent = new Intent();
        ETHERNET = cm.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET).getState();
  if(ETHERNET!=State.UNKNOWN) {


      if (ETHERNET != null && State.CONNECTED == ETHERNET) {
          mIntent.setAction(PushAction.ACTION_ENTHERNET_CONNECT);

      }else if(wifiState != null && State.CONNECTED == wifiState){
          mIntent.setAction(PushAction.ACTION_WIFI_CONNECT);
      }

      else if (ETHERNET != null && State.DISCONNECTED == ETHERNET && wifiState != null
              && State.DISCONNECTED == wifiState) {
          mIntent.setAction(PushAction.ACTION_NETWORK_DISCONNECT);
      }
  }else {
      if (wifiState != null && mobileState != null
              && State.CONNECTED != wifiState
              && State.CONNECTED == mobileState) {
          ActivityContents.IsNetWorkAvailable = true;
          // 手机网络连接成功
      } else if (wifiState != null && mobileState != null
              && State.DISCONNECTED == wifiState
              && State.DISCONNECTED == mobileState) {
          ActivityContents.IsNetWorkAvailable = false;
          // 手机没有任何的网络
      } else if (wifiState != null && State.CONNECTED == wifiState) {
          // 无线网络连接成功
          ActivityContents.IsNetWorkAvailable = true;
          mIntent.setAction(PushAction.ACTION_WIFI_CONNECT);

      } else if (wifiState != null
              && State.DISCONNECTED == wifiState) {
          ActivityContents.IsNetWorkAvailable = false;
          mIntent.setAction(PushAction.ACTION_NETWORK_DISCONNECT);
          // 手机没有任何的网络
      } else if (mobileState != null
              && State.CONNECTED == mobileState) {
          ActivityContents.IsNetWorkAvailable = true;
          // 手机网络连接成功
      } else if (mobileState != null
              && State.DISCONNECTED == mobileState) {
          ActivityContents.IsNetWorkAvailable = false;
          // 手机没有任何的网络
      }
  }
        LocalBroadcastManager.getInstance(context).sendBroadcast(mIntent);
};
}   
}

第四步:清单文件中配置

    <receiver
        android:name=".MultiModel.WifiModel.NetworkStateChange"  >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

相关文章

网友评论

      本文标题:LocalBroadcast

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