Android Development Network detection (using broadcast monitoring)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.