First step: Configure Androidmanifest.xml, add the following code below the application node
<receiverAndroid:name=". Networkconnectchangedreceiver " > <Intent-filter> <ActionAndroid:name= "Android.net.conn.CONNECTIVITY_CHANGE" /> <ActionAndroid:name= "Android.net.wifi.WIFI_STATE_CHANGED" /> <ActionAndroid:name= "Android.net.wifi.STATE_CHANGE" /> </Intent-filter></receiver>
Also configure Androidmanifest.xml, add permissions
<android:name= "Android.permission.ACCESS_NETWORK_STATE"/> <android:name= "Android.permission.ACCESS_COARSE_LOCATION"/> < android:name= "Android.permission.ACCESS_WIFI_STATE"/>
Step Two: Register for the broadcast
Intentfilter filter =NewIntentfilter ();/*** network_state_changed_action * * WiFi connection is OK **/filter.addaction (wifimanager.network_state_changed_action);/*** wifi_state_changed_action * * There is no WIFI source and changes between the various states **/filter.addaction (wifimanager.wifi_state_changed_action);/*** connectivity_action * wifi if open, close, and connections available on the connection will be monitored * The biggest drawback of this broadcast is slower than the top two radio response, if only to listen to WiFi, I think it is more appropriate to use the top two matching * */filter.addaction (connectivitymanager.connectivity_action); Registerreceiver (NewNetworkconnectchangedreceiver (NewNetworkconnectchangedactionimp ()), filter);
Step Three:
ImportAndroid.content.Context;Importandroid.content.Intent; Public Interfacenetworkconnectchangedaction {voiddoafterconnected (Context context, Intent Intent); voiddoafterconnecting (Context context, Intent Intent); voiddoafterdisconnected (Context context, Intent Intent); voiddoafterdisconnecting (Context context, Intent Intent); voiddoaftersuspended (Context context, Intent Intent); voidDoafterunknown (Context context, Intent Intent);}
ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.net.ConnectivityManager;ImportAndroid.net.NetworkInfo; Public classNetworkconnectchangedreceiverextendsBroadcastreceiver {Private StaticNetworkconnectchangedaction action =NULL; Static{Action=NewNetworkconnectchangedactionimp (); } @Override Public voidOnReceive (Context context, Intent Intent) {if(ConnectivityManager.CONNECTIVITY_ACTION.equals (Intent.getaction ())) {Connectivitymanager Manager=(Connectivitymanager) Context.getsystemservice (Context.connectivity_service); /*** GPRS*/ //Networkinfo GPRS = Manager.getnetworkinfo (connectivitymanager.type_mobile); /*** INFO*/ //Networkinfo info = Intent.getparcelableextra (connectivitymanager.extra_network_info); /*** WIFI*/Networkinfo WiFi=Manager.getnetworkinfo (Connectivitymanager.type_wifi); if(WiFi! =NULL) { if(NetworkInfo.State.CONNECTED = =wifi.getstate ()) {action.doafterconnected (context, intent); } Else if(NetworkInfo.State.CONNECTING = =wifi.getstate ()) {action.doafterconnecting (context, intent); } Else if(NetworkInfo.State.DISCONNECTED = =wifi.getstate ()) {action.doafterdisconnected (context, intent); } Else if(NetworkInfo.State.DISCONNECTING = =wifi.getstate ()) {action.doafterdisconnecting (context, intent); } Else if(NetworkInfo.State.SUSPENDED = =wifi.getstate ()) {action.doaftersuspended (context, intent); } Else if(NetworkInfo.State.UNKNOWN = =wifi.getstate ()) {Action.doafterunknown (context, intent); } } } }}
Fourth Step: Concrete Implementation
ImportAndroid.content.Context;Importandroid.content.Intent; Public classNetworkconnectchangedactionimpImplementsnetworkconnectchangedaction{@Override Public voiddoafterconnected (Context context, Intent Intent) {} @Override Public voiddoafterconnecting (Context context, Intent Intent) {} @Override Public voiddoafterdisconnected (Context context, Intent Intent) {} @Override Public voiddoafterdisconnecting (Context context, Intent Intent) {} @Override Public voiddoaftersuspended (Context context, Intent Intent) {} @Override Public voidDoafterunknown (Context context, Intent Intent) {}}