Android to determine the network connection

Source: Internet
Author: User

1 ImportAndroid.content.Context;2 ImportAndroid.net.ConnectivityManager;3 ImportAndroid.net.NetworkInfo;4 5 /**6 * Network Tool class Add network status license: <uses-permission7 * android:name= "Android.permission.ACCESS_NETWORK_STATE"/>8  * 9  * @authorBennyTen  *  One  */ A  Public classNetworkutil { -  -     /** the * Network Connection Status - * <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/> -      * @paramContext - * Program Context +      * @returntrue: Can connect -      */ +      Public Static Booleanisnetworkconnected (Context context) { A         if(Context! =NULL) { atConnectivitymanager Connectivitymanager =(Connectivitymanager) Context.getsystemservice (context.connectivity_service); -  -Networkinfo Networkinfo =connectivitymanager.getactivenetworkinfo (); -             if(Networkinfo! =NULL) { -                 returnnetworkinfo.isavailable (); -             } in         } -         return false; to     } +  -     /** the * WiFi network connection Status * * <uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/> $      * @paramContextPanax Notoginseng * Program Context -      * @returntrue: Can connect the      */ +      Public Static Booleaniswificonnected (Context context) { A         if(Context! =NULL) { theConnectivitymanager Connectivitymanager =(Connectivitymanager) Context.getsystemservice (context.connectivity_service); +  -Networkinfo Networkinfo =Connectivitymanager.getnetworkinfo (Connectivitymanager.type_wifi); $             if(Networkinfo! =NULL) { $                 returnnetworkinfo.isavailable (); -             } -         } the         return false; -     }Wuyi  the     /** - * Mobile Network connection Status Wu * <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/> -      *  About      * @paramContext $ * Program Context -      * @returntrue: Can connect -      */ -      Public Static Booleanismobileconnected (Context context) { A         if(Context! =NULL) { +Connectivitymanager Connectivitymanager =(Connectivitymanager) Context.getsystemservice (context.connectivity_service); the  -Networkinfo Networkinfo =connectivitymanager.getactivenetworkinfo (); $             if(Networkinfo! =NULL&& Networkinfo.gettype () = =connectivitymanager.type_mobile) { the                     returnnetworkinfo.isavailable (); the             } the         } the         return false; -     } in  the     /** the * Get network connection type About      * @paramContext Program Contexts the      * @returnconnectivitymanager.type_xxxx the      */ the      Public Static intGetconnectedtype (Context context) { +         if(Context! =NULL) { -Connectivitymanager Connectivitymanager =(Connectivitymanager) Context.getsystemservice (context.connectivity_service); theNetworkinfo Mnetworkinfo =connectivitymanager.getactivenetworkinfo ();Bayi             if(Mnetworkinfo! =NULL&&mnetworkinfo.isavailable ()) { the                 returnMnetworkinfo.gettype (); the             } -         } -         return-1; the     } the  the}

Android to determine the network connection

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.