1. Declaration in XML
<Cycler Android: Name = ". networkconnectchangedreceiver">
<Intent-filter>
<Action Android: Name = "android.net. Conn. connectivity_change"/>
<Action Android: Name = "android.net. Wifi. wifi_state_changed"/>
<Action Android: Name = "android.net. Wifi. state_change"/>
</Intent-filter>
</Cycler>
2. Register in the code
Intentfilter filter = new intentfilter ();
Filter. addaction (wifimanager. network_state_changed_action );
Filter. addaction (wifimanager. wifi_state_changed_action );
Filter. addaction (connectivitymanager. connectivity_action );
Registerreceiver (New networkconnectchangedreceiver (), filter );
3. Implementation
Package COM. example. testss; import android. content. broadcastreceiver; import android. content. context; import android. content. intent; import android.net. connectivitymanager; import android.net. networkinfo; import android.net. networkinfo. state; import android.net. wiFi. wifimanager; import android. OS. parcelable; import android. util. log; public class networkconnectchangedreceiver extends broadcastreceiver {@ over Ride public void onreceive (context, intent) {If (wifimanager. wifi_state_changed_action.equals (intent. getaction () {// enable and disable the listener wifi. It has nothing to do with the connection of the WiFi. Int wifistate = intent. getintextra (wifimanager. extra_wifi_state, 0); log. E ("H3C", "wifistate" + wifistate); Switch (wifistate) {Case wifimanager. wifi_state_disabled: break; Case wifimanager. wifi_state_disabling: break; //} // This listens to the Wi-Fi The connection status indicates whether a valid wireless route is connected. When the broadcast status is wifimanager. wifi_state_disabling and wifi_state_disabled, the broadcast is not received. // Wifimanager is used to broadcast data to IOT platform. wifi_state_enabled status will also receive this broadcast, of course, just opened WiFi is certainly not connected to a valid wireless if (wifimanager. network_state_changed_action.equals (intent. getaction () {parcelable parcelableextra = intent. getparcelableextra (wifimanager. extra_network_info); If (null! = Parcelableextra) {networkinfo = (networkinfo) parcelableextra; State state = networkinfo. getstate (); Boolean isconnected = State = state. connected; // Of course, you can determine the status log more accurately. E ("H3C", "isconnected" + isconnected); If (isconnected) {} else {}}// set the network connection for this listener, including enabling and disabling WiFi and mobile data .. // It is best to use this listener. If WiFi is enabled, disabled, and connections available on the connection are listened. See log // The biggest drawback of this broadcast is that it is slower than the response of the above two broadcasts. If you only want to listen to wifi, I think it is more appropriate to use the above two combinations if (connectivitymanager. connectivity_action.equals (intent. getaction ())){Connectivitymanager manager = (connectivitymanager) Context. getsystemservice (context. connectivity_service); networkinfo GPRS = manager. getnetworkinfo (connectivitymanager. type_mobile); networkinfo WiFi = manager. getnetworkinfo (connectivitymanager. type_wifi );
Log. I (TAG, "network status changed:" + wifi. isconnected () + "3G:" + GPRS. isconnected ());
Networkinfo info = intent. getparcelableextra (connectivitymanager. extra_network_info); If (info! = NULL) {log. E ("H3C", "info. gettypename () "+ info. gettypename (); log. E ("H3C", "getsubtypename ()" + info. getsubtypename (); log. E ("H3C", "getstate ()" + info. getstate (); log. E ("H3C", "getdetailedstate ()" + info. getdetailedstate (). name (); log. E ("H3C", "getdetailedstate ()" + info. getextrainfo (); log. E ("H3C", "GetType ()" + info. getType (); If (networkinfo. state. connected = info. getstate () {} else if (info. getType () = 1) {If (networkinfo. state. disconnecting = info. getstate ()){}}}}}}