[A basket of meals is very rare and original. For more information, see http://www.cnblogs.com/wanghafan/archive/2013/01/10/2855096.html]
1. Register a broadcast receiver to listen to the network status
1 private void Start()2 {3 IntentFilter filter = new IntentFilter();4 filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);5 filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);6 filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);7 this.registerReceiver(myBroadcastReceiver, filter);8 }
2. Cancel registering the broadcaster
1 private void Stop()2 {3 this.unregisterReceiver(myBroadcastReceiver);4 }
3. The new listener class NetworkConnectChangedReceiver inherits BroadcastReceiver.
1 public class networkconnectchangedreceiver extends broadcastreceiver 2 {3 @ override 4 Public void onreceive (context, intent) 5 {6 // This listener enables and disables wifi, it has nothing to do with Wi-Fi connection. 7 if (wifimanager. wifi_state_changed_action.equals (intent. getaction () 8 {9 int wifistate = intent. getintextra (wifimanager. extra_wifi_state, 0); 10 switch (wifistate) 11 {12 case wifimanager. wifi_state_enabled: 13 log. E ("Apa Ctivity "," wifi_state_enabled "); 14 break; 15 case wifimanager. wifi_state_enabling: 16 log. E ("apactivity", "wifi_state_enabling"); 17 break; 18 case wifimanager. wifi_state_disabled: 19 log. E ("apactivity", "wifi_state_disabled"); 20 break; 21 case wifimanager. wifi_state_disabling: 22 log. E ("apactivity", "wifi_state_disabling"); 23 break; 24} 25} 26 // whether a valid wireless route is connected to the connection status of the listener wifi, when the status of the broadcast above is wifimanager. wifi_stat E_disabling, and wifi_state_disabled are not broadcast at all. 27 // broadcast to the broadcast above is wifimanager. wifi_state_enabled status will also receive this broadcast, of course, just opened WiFi is certainly not connected to a valid wireless 28 If (wifimanager. network_state_changed_action.equals (intent. getaction () 29 {30 parcelable parcelableextra = intent. getparcelableextra (wifimanager. extra_network_info); 31 if (null! = Parcelableextra) 32 {33 networkinfo = (networkinfo) parcelableextra; 34 switch (networkinfo. getstate () 35 {36 case connected: 37 log. E ("apactivity", "connected"); 38 break; 39 case connecting: 40 log. E ("apactivity", "connecting"); 41 break; 42 case disconnected: 43 log. E ("apactivity", "disconnected"); 44 break; 45 case disconnecting: 46 log. E ("apactivity", "disconnecting"); 47 break; 48 case suspen DED: 49 log. E ("apactivity", "suincluded"); 50 break; 51 Case unknown: 52 log. E ("apactivity", "unknown"); 53 break; 54 default: 55 break; 56} 57} 58} 59 // set the network connection for this listener, including enabling and disabling WiFi and mobile data. 60 // it is best to use this listener. If WiFi is enabled, disabled, and connections available on the connection are listened. See log61 // The biggest drawback of this broadcast is that it is slower than the previous two broadcasts. If you only want to listen to wifi, I think it is more appropriate to use the above two broadcasts. 62 if (connectivitymanager. connectivity_action.equals (intent. getaction () 63 {64 networkinfo info = intent. getparcelableextra (connectivitymanager. extra_network_info); 65 if (info! = NULL) 66 {67 stringbuilder sb = new stringbuilder (); 68 sb. append ("info. gettypename (): "+ info. gettypename () + "\ n"); 69 sb. append ("getsubtypename ():" + info. getsubtypename () + "\ n"); 70 sb. append ("getstate ():" + info. getstate () + "\ n"); 71 sb. append ("getdetailedstate ():" + info. getdetailedstate (). name () + "\ n"); 72 sb. append ("getdetailedstate ():" + info. getextrainfo () + "\ n"); 73 sb. append ("GetType ():" + info. getType (); 74 log. E ("apactivity", sb. tostring (); 75} 76} 77} 78}