亲测Android wifi状态三种广播

测试代码
[java] view plain copy print ?
  1. public class NetworkConnectChangedReceiver extends BroadcastReceiver{  
  2.     @Override  
  3.     public void onReceive(Context context, Intent intent) {  
  4.     if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {//这个监听wifi的打开与关闭,与wifi的连接无关  
  5.             int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);   
  6.             LogTag.showTAG_e("WIFI状态""wifiState"+wifiState);  
  7.             switch (wifiState) {   
  8.             case WifiManager.WIFI_STATE_DISABLED:   
  9.                 break;   
  10.             case WifiManager.WIFI_STATE_DISABLING:   
  11.                 break;   
  12.            //...  
  13.             }   
  14.         }  
  15.     // 这个监听wifi的连接状态即是否连上了一个有效无线路由,当上边广播的状态是WifiManager.WIFI_STATE_DISABLING,和WIFI_STATE_DISABLED的时候,根本不会接到这个广播。  
  16.     // 在上边广播接到广播是WifiManager.WIFI_STATE_ENABLED状态的同时也会接到这个广播,当然刚打开wifi肯定还没有连接到有效的无线  
  17.     if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {  
  18.             Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);    
  19.             if (null != parcelableExtra) {    
  20.                 NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;    
  21.                 State state = networkInfo.getState();  
  22.                 boolean isConnected = state==State.CONNECTED;//当然,这边可以更精确的确定状态  
  23.                 LogTag.showTAG_e(this.getClass().getSimpleName(), "isConnected"+isConnected);  
  24.                 if(isConnected){  
  25.                 }else{  
  26.                       
  27.                 }  
  28.             }    
  29.         }  
  30.     //这个监听网络连接的设置,包括wifi和移动数据的打开和关闭。.   
  31.         //最好用的还是这个监听。wifi如果打开,关闭,以及连接上可用的连接都会接到监听。见log  
  32.         // 这个广播的最大弊端是比上边两个广播的反应要慢,如果只是要监听wifi,我觉得还是用上边两个配合比较合适  
  33.     if(ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())){  
  34.         NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);  
  35.         if (info != null) {  
  36.             LogTag.showTAG_e("CONNECTIVITY_ACTION""info.getTypeName()"+info.getTypeName());  
  37.             LogTag.showTAG_e("CONNECTIVITY_ACTION""getSubtypeName()"+info.getSubtypeName());  
  38.             LogTag.showTAG_e("CONNECTIVITY_ACTION""getState()"+info.getState());  
  39.             LogTag.showTAG_e("CONNECTIVITY_ACTION",  
  40.                                 "getDetailedState()"+info.getDetailedState().name());  
  41.             LogTag.showTAG_e("CONNECTIVITY_ACTION""getDetailedState()"+info.getExtraInfo());  
  42.             LogTag.showTAG_e("CONNECTIVITY_ACTION""getType()"+info.getType());  
  43.         }   
  44.     }  
  45.   
  46. /** 
  47. 08-29 19:51:30.366: E/CONNECTIVITY_ACTION(16920): ========info.getTypeName()WIFI============= 
  48. 08-29 19:51:30.366: E/CONNECTIVITY_ACTION(16920): ========getSubtypeName()============= 
  49. 08-29 19:51:30.366: E/CONNECTIVITY_ACTION(16920): ========getState()DISCONNECTED============= 
  50. 08-29 19:51:30.366: E/CONNECTIVITY_ACTION(16920): ========getDetailedState()DISCONNECTED============= 
  51. 08-29 19:51:30.366: E/CONNECTIVITY_ACTION(16920): ========getDetailedState()null============= 
  52. 08-29 19:51:30.366: E/CONNECTIVITY_ACTION(16920): ========getType()1============= 
  53. --------------- 
  54. 08-29 19:52:35.757: E/CONNECTIVITY_ACTION(16920): ========info.getTypeName()mobile============= 
  55. 08-29 19:52:35.757: E/CONNECTIVITY_ACTION(16920): ========getSubtypeName()HSPA============= 
  56. 08-29 19:52:35.757: E/CONNECTIVITY_ACTION(16920): ========getState()CONNECTED============= 
  57. 08-29 19:52:35.757: E/CONNECTIVITY_ACTION(16920): ========getDetailedState()CONNECTED============= 
  58. 08-29 19:52:35.757: E/CONNECTIVITY_ACTION(16920): ========getDetailedState()3gnet============= 
  59. 08-29 19:52:35.757: E/CONNECTIVITY_ACTION(16920): ========getType()0============= 
  60.          */  
  61.     }  
  62.   
  63.   
  64. }  
最终监听wifi提示的代码
[java] view plain copy print ?
  1. if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {// 这个监听wifi的连接状态  
  2.            Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);    
  3.            if (null != parcelableExtra) {    
  4.                NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;    
  5.                State state = networkInfo.getState();  
  6.                if(state==State.CONNECTED){  
  7.                 showWifiCconnected(context);  
  8.                }  
  9.                /**else if(state==State.DISCONNECTED){ 
  10.                 showWifiDisconnected(context); 
  11.                }*///昨天写的这个方法,在坐地铁的时候发现,如果地铁上有无效的wifi站点,手机会自动连接,但是连接失败后还是会接到广播,所以不能用了  
  12.            }    
  13.        }  
  14. if(ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())){//这个监听网络连接的设置,包括wifi和移动数据 的打开和关闭  
  15. NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);  
  16. if (info != null) {  
  17.     if(NetworkInfo.State.CONNECTED==info.getState()){  
  18.         Intent pushIntent = new Intent();  
  19.         pushIntent.setClass(context, NotificationService.class);  
  20.     }else if(info.getType()==1){  
  21.             if(NetworkInfo.State.DISCONNECTING==info.getState())  
  22.                 showWifiDisconnected(context);  
  23.         }  
  24.     }   
  25. }  
  26.        }  

打开和关闭2g/3g网络,以及wifi收到的日志信息

[plain] view plain copy print ?
  1. 2g打开情况,关闭wifi  
  2. 07-10 20:30:57.764: I/System.out(24781): NetworkInfo: type: WIFI[], state: DISCONNECTED/DISCONNECTED, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: false  
  3. 07-10 20:31:00.460: I/System.out(24781): NetworkInfo: type: mobile[HSPA], state: CONNECTED/CONNECTED, reason: apnSwitched, extra: 3gnet, roaming: false, failover: false, isAvailable: true  
  4.   
  5. wifi关闭情况,关闭2g  
  6. 07-10 20:31:11.366: I/System.out(24781): NetworkInfo: type: mobile[HSPA], state: DISCONNECTED/DISCONNECTED, reason: dataDisabled, extra: 3gnet, roaming: false, failover: false, isAvailable: true  
  7.   
  8. wifi关闭情况,打开2g  
  9. 07-10 20:33:10.913: I/System.out(24781): NetworkInfo: type: mobile[HSPA], state: CONNECTED/CONNECTED, reason: apnSwitched, extra: 3gnet, roaming: false, failover: false, isAvailable: true  
  10.   
  11.   
  12.   
  13. 2G打开情况,打开wifi  
  14. 07-10 20:33:43.764: I/System.out(24781): NetworkInfo: type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true  
  15.   
  16. 07-10 20:33:44.272: I/System.out(24781): NetworkInfo: type: mobile[HSPA], state: DISCONNECTED/DISCONNECTED, reason: dataDisabled, extra: 3gnet, roaming: false, failover: false, isAvailable: true  
  17.   
  18. 07-10 20:33:44.569: I/System.out(24781): NetworkInfo: type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true  
  19.   
  20. wifi 打开情况,关闭2g  
  21. 无  
  22.   
  23.   
  24. 2g关闭情况,关闭wifi  
  25. 07-10 20:36:00.702: I/System.out(24781): NetworkInfo: type: WIFI[], state: DISCONNECTED/DISCONNECTED, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: false  
  26.   
  27. 2G关闭情况,打开wifi  
  28. 07-10 20:38:31.632: I/System.out(25310): NetworkInfo: type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true   info.getType 1 

你可能感兴趣的:(亲测Android wifi状态三种广播)