1.XML Declaration
<receiver 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>
</receiver>
2. Register in 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. To achieve
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 {@Override public void onreceive (context con Text, Intent Intent) {if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals (Intent.getaction ())) {//This listening WIFI is turned on and off, with W
IFI connection Independent 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 listening WiFi connection state is connected to a valid no line, when the state of the top broadcast is WifimanageR.wifi_state_disabling, and wifi_state_disabled, will not receive this broadcast at all. Broadcast at the top of the broadcast is wifimanager.wifi_state_enabled state and will also receive this broadcast, of course, just open WIFI must not be connected to a valid wireless if (Wifimanager.network_state_cha Nged_action.equals (Intent.getaction ())) {parcelable Parcelableextra = intent. Getparcelab
Leextra (Wifimanager.extra_network_info);
if (null!= parcelableextra) {networkinfo networkinfo = (networkinfo) Parcelableextra;
state state = Networkinfo.getstate (); Boolean isconnected = state = = state.connected;//Of course, this side can be more accurate to determine the status log.e ("H3C", "isconnected" + isconnected
); if (isconnected) {} else {}}}//This listener network connection settings, including WiFi and mobile data
Open and close. The best thing to use is this listening. WiFi if turned on, turned off, and connections available on the connection will be monitored. See Log//The biggest disadvantage of this broadcast is slower than the two broadcasts on the top, if only to monitor WiFi, I think it is better to use the top two fit if (ConnectivityManager.CONNECTIVITY_ACTION.eq Uals (intent.getactiOn ()) {<p> Connectivitymanager manager = (connectivitymanager) context. Getsystemservice (context.connectivity_
SERVICE);
Networkinfo GPRS = Manager. Getnetworkinfo (Connectivitymanager.type_mobile); Networkinfo WiFi = Manager. Getnetworkinfo (Connectivitymanager.type_wifi);</p><p> log.i (TAG, "Network state Change:" + W
ifi.isconnected () + "3g:" + gprs.isconnected ());</p><p> 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 ()) {}}} }}}</p>