A relatively simple detection network:
1 /**2 * Check if the network is connected3 * @paramContext4 * @return5 */6 Public Booleanisnetworkconnected (Context context) {7 if(Context! =NULL) {8Connectivitymanager Mconnectivitymanager =(connectivitymanager) Context9 . Getsystemservice (context.connectivity_service);Ten //get valid network information OneNetworkinfo Mnetworkinfo =Mconnectivitymanager A . Getactivenetworkinfo (); - if(Mnetworkinfo! =NULL) { - //returns whether the network is available the returnmnetworkinfo.isavailable (); - } - } - return false; +}
The network connection status is detected by means of a broadcast and the name of the access point:
1 /**2 * Network connection broadcast receive3 * @authorAdministrator4 *5 */6 Private classChecknetstaterecevicerextendsBroadcastreceiver {7 8 @Override9 Public voidOnReceive (Context context, Intent Intent) {Ten //TODO auto-generated Method Stub One if(Intent.getaction (). Equals ( A connectivitymanager.connectivity_action)) { -LOG.I ("Info", "network status has changed"); -CM = (Connectivitymanager) getsystemservice (Context.connectivity_service);//access to network connectivity Services theNetworkinfo = Cm.getactivenetworkinfo ();//get the available network information - - if(Networkinfo! =NULL -&& Networkinfo.gettype () = =connectivitymanager.type_mobile) { +String netId = Networkinfo.getextrainfo ();//get the network mode of the mobile Access point -Toast.maketext (context, "Network connected to" +netId, Toast.length_short) + . Show (); A}Else if(Networkinfo! =NULL at&& Networkinfo.gettype () = =Connectivitymanager.type_wifi) { -Wifimanager WM =(Wifimanager) Getsystemservice (context.wifi_service); -String SSID = Wm.getconnectioninfo (). Getssid (). toString ();//get the name of the WiFi access point -Toast.maketext (context, "Network connected to" +SSID, Toast.length_short) - . Show (); -}Else { inToast.maketext (context, "Network unexpected", Toast.length_long). Show (); - } to } + - } the}
Android Development Network detection (using broadcast monitoring)