18_ Mobile network Connection Status

Source: Internet
Author: User

Determine the network connection status of your phone to identify mobile and WiFi connections.

The following procedure, refer to from http://www.cnblogs.com/qingblog/archive/2012/07/19/2598983.html

Obtaining network information requires that the appropriate permissions be added to the Androidmanifest.xml file.

<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>

1 /**2 * Determine network connection Status3 * 1. Mobile network Connection4 * 2. WiFi Internet connection5 * 3. No Internet connection6  * 7 * The method of judging is: use Connectivitymanager to get networkinfo. 8  * 9 * Several functions are returned at the back of the programTen * The following procedures, reference fromhttp://www.cnblogs.com/qingblog/archive/2012/07/19/2598983.html One  */ A  -  Packagecom.example.networkstate; -  the Importandroid.app.Activity; - ImportAndroid.content.BroadcastReceiver; - ImportAndroid.content.Context; - Importandroid.content.Intent; + ImportAndroid.content.IntentFilter; - ImportAndroid.net.ConnectivityManager; + ImportAndroid.net.NetworkInfo; A ImportAndroid.os.Bundle; at ImportAndroid.widget.TextView; -  -  Public classMainactivityextendsActivity { -  -     Privatebroadcastreceiver Connectionreceiver; -     PrivateTextView tvnetworkstate; in  - @Override to     protected voidonCreate (Bundle savedinstancestate) { +         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); the  *Tvnetworkstate =(TextView) Findviewbyid (r.id.tvnetworkstate); $ Panax NotoginsengConnectionreceiver =NewBroadcastreceiver () { -  the @Override +              Public voidOnReceive (Context context, Intent Intent) { AConnectivitymanager connectmgr =(Connectivitymanager) Getsystemservice (connectivity_service); theNetworkinfo Mobnetinfo =Connectmgr + . Getnetworkinfo (connectivitymanager.type_mobile); -Networkinfo Wifinetinfo =Connectmgr $ . Getnetworkinfo (Connectivitymanager.type_wifi); $  -                 if(mobnetinfo.isconnected ()) { -Tvnetworkstate.settext ("Phone network connection ~"); the}Else if(wifinetinfo.isconnected ()) { -Tvnetworkstate.settext ("WiFi Network connection ~");Wuyi}Else { theTvnetworkstate.settext ("No Internet connection ~"); -                 } Wu  -                 //if (!mobnetinfo.isconnected () &&!wifinetinfo.isconnected ()) About                 // { $                 // //Unconnect Network -                 //Tvnetworkstate.settext ("No network connection at this moment!") "); -                 //} else { -                 // //Connect Network A                 //Tvnetworkstate.settext ("Congratulations, there is Internet connection ~"); +                 // } the             } -  $         }; the  theIntentfilter Intentfilter =NewIntentfilter (); the intentfilter.addaction (connectivitymanager.connectivity_action); the registerreceiver (Connectionreceiver, intentfilter); -     } in  the @Override the     protected voidOnDestroy () { About         Super. OnDestroy (); the         if(Connectionreceiver! =NULL) { the Unregisterreceiver (connectionreceiver); the         } +     } -  the      Public Booleanisnetworkconneted (Context context) {Bayi         if(Context! =NULL) { theConnectivitymanager Connectivitymanager =(connectivitymanager) Context the . Getsystemservice (context.connectivity_service); -Networkinfo Networkinfo =Connectivitymanager - . Getactivenetworkinfo (); the             if(Networkinfo! =NULL) { the                 returnnetworkinfo.isavailable (); the             } the         } -         return false; the     } the  the      Public Booleaniswificonnected (Context context) {94         if(Context! =NULL) { theConnectivitymanager Connectivitymanager =(connectivitymanager) Context the . Getsystemservice (context.connectivity_service); theNetworkinfo Networkinfo =Connectivitymanager98 . Getnetworkinfo (Connectivitymanager.type_wifi); About             if(Networkinfo! =NULL) { -                 returnnetworkinfo.isavailable ();101             }102         }103         return false;104     } the 106      Public Booleanismobileconnected (Context context) {107         if(Context! =NULL) {108Connectivitymanager Mconnectivitymanager =(connectivitymanager) Context109 . Getsystemservice (context.connectivity_service); theNetworkinfo Mmobilenetworkinfo =Mconnectivitymanager111 . Getnetworkinfo (connectivitymanager.type_mobile); the             if(Mmobilenetworkinfo! =NULL) {113                 returnmmobilenetworkinfo.isavailable (); the             } the         } the         return false;117     }118 119      Public Static intGetconnectedtype (Context context) { -         if(Context! =NULL) {121Connectivitymanager Mconnectivitymanager =(connectivitymanager) Context122 . Getsystemservice (context.connectivity_service);123Networkinfo Mnetworkinfo =Mconnectivitymanager124 . Getactivenetworkinfo (); the             if(Mnetworkinfo! =NULL&&mnetworkinfo.isavailable ()) {126                 returnMnetworkinfo.gettype ();127             } -         }129         return-1; the     }131}

18_ Mobile network Connection Status

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.