首页 > 代码库 > Wi-Fi广播监听

Wi-Fi广播监听

/**
* Wifi广播接收
*/
private BroadcastReceiver mNetworkStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction() == WifiManager.NETWORK_STATE_CHANGED_ACTION){
NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
WifiInfo wifiInfo = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO);
Log.d(TAG, "networkInfo="+networkInfo);
Log.d(TAG, "wifiInfo="+wifiInfo);
if(networkInfo!=null){
if(networkInfo.isAvailable()){
if(networkInfo.getType()==ConnectivityManager.TYPE_WIFI){
closeWifiCloseView();
Log.d(TAG, "networkInfo="+networkInfo.isAvailable());
Log.d(TAG, "networkInfo="+networkInfo.getState());
if(networkInfo.getState() == State.CONNECTED){
isConnWifiing = false;
setViewData();
if (!isLocationing && RuntimeUtils.bdLocation == null) {
isGoRefresh = true;
starLocation();
}
if (nearAbleWiFiFrg != null && !nearAbleWiFiFrg.isHidden()) {
nearAbleWiFiFrg.notifyWifiAble();
nearAbleWiFiFrg.updataWifiList();
}
if (nearAbleIcengWiFiFrg != null) {
nearAbleIcengWiFiFrg.updataWifiList();
}
}else if(networkInfo.getState() == State.CONNECTING){
if(wifiInfo!=null){
setConningView(wifiInfo.getSSID());
}
}else if(networkInfo.getState() == State.DISCONNECTED){
setDisConnWifi();
}else if(networkInfo.getState() == State.DISCONNECTING){
}
}else{
setWifiCloseView();
}
}else{
setDisConnWifi();
}
}
}
if(intent.getAction() == WifiManager.WIFI_STATE_CHANGED_ACTION){
switch (intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0)) {
case WifiManager.WIFI_STATE_DISABLED:
Log.d(TAG, "WIFI_STATE_DISABLED=");
break;
case WifiManager.WIFI_STATE_DISABLING:
Log.d(TAG, "WIFI_STATE_DISABLING=");
break;
case WifiManager.WIFI_STATE_ENABLED:
Log.d(TAG, "WIFI_STATE_ENABLED=");
break;
case WifiManager.WIFI_STATE_ENABLING:
Log.d(TAG, "WIFI_STATE_ENABLING=");
break;
default:
break;
}
}
// if(intent.getAction() == ConnectivityManager.CONNECTIVITY_ACTION){
// if (NetworkUtils.isNetAble(mContext)) {
// if (WifiConnect.getIntance().isOpened()) {// wifi打开
// closeWifiCloseView();
// State wifiState = WifiConnect.getIntance().getWifiState();
// Log.d(TAG, "State=" + wifiState.name());
// if (wifiState == State.CONNECTED) {
// isConnWifiing = false;
// setViewData();
// if (!isLocationing && RuntimeUtils.bdLocation == null) {
// isGoRefresh = true;
// starLocation();
// }
// if (nearAbleWiFiFrg != null && !nearAbleWiFiFrg.isHidden()) {
// nearAbleWiFiFrg.notifyWifiAble();
// nearAbleWiFiFrg.updataWifiList();
// }
// if (nearAbleIcengWiFiFrg != null) {
// nearAbleIcengWiFiFrg.updataWifiList();
// }
// } else if (wifiState == State.CONNECTING) {
// setConnectedIceng();
// } else if (wifiState == State.DISCONNECTED) {
// setDisConnIceng();
// }
// }else{
// setWifiCloseView();
// }
// } else {//无wifi|3G
// setDisConnIceng();
// //setWifiCloseView();
// //gotoNetworkErrorPage();
// }
// }
}
};

Wi-Fi广播监听