Android development tool Class 18_networkutil

Source: Internet
Author: User

A toolkit for detecting networks:

1, the network is available;

2, determine whether there is network connection;

3, determine whether the WiFi network is available;

4, determine whether MOBILE network is available;

5, obtain the current network connection type information;

6, get the current network status-1: No network 1:wifi network 2:wap network 3:net network;

1 ImportAndroid.content.Context;2 ImportAndroid.net.ConnectivityManager;3 ImportAndroid.net.NetworkInfo;4 5 /**6 * @Title Networkutil7 * @Description is a toolkit for detecting networks8  * @authorCalm9  */Ten  Public classNetworkutil { One      A      Public Static enumNetType { - WIFI, CMNET, Cmwap, Nonenet -     } the  -     /** - * Whether the network is available -      *  +      * @paramContext -      * @return +      */ A      Public Static Booleanisnetworkavailable (Context context) { at          -Connectivitymanager mgr =(Connectivitymanager) Context.getsystemservice (context.connectivity_service); -Networkinfo[] Info =mgr.getallnetworkinfo (); -         if(Info! =NULL) { -              for(inti = 0; i < info.length; i++) { -                 if(info[i].getstate () = =NetworkInfo.State.CONNECTED) { in                     return true; -                 } to             } +         } -         return false; the     } *  $     /**Panax Notoginseng * Determine if there is an Internet connection -      *  the      * @paramContext +      * @return A      */ the      Public Static Booleanisnetworkconnected (Context context) { +         if(Context! =NULL) { -Connectivitymanager Mconnectivitymanager =(Connectivitymanager) Context.getsystemservice (context.connectivity_service); $Networkinfo Mnetworkinfo =mconnectivitymanager.getactivenetworkinfo (); $             if(Mnetworkinfo! =NULL) { -                 returnmnetworkinfo.isavailable (); -             } the         } -         return false;Wuyi     } the  -     /** Wu * Determine if WiFi network is available -      *  About      * @paramContext $      * @return -      */ -      Public Static Booleaniswificonnected (Context context) { -          A         if(Context! =NULL) { +Connectivitymanager Mconnectivitymanager =(Connectivitymanager) Context.getsystemservice (context.connectivity_service); theNetworkinfo Mwifinetworkinfo =Mconnectivitymanager.getnetworkinfo (Connectivitymanager.type_wifi); -             if(Mwifinetworkinfo! =NULL) { $                 returnmwifinetworkinfo.isavailable (); the             } the         } the         return false; the     } -  in     /** the * Determine if MOBILE network is available the      *  About      * @paramContext the      * @return the      */ the      Public Static Booleanismobileconnected (Context context) { +         if(Context! =NULL) { -Connectivitymanager Mconnectivitymanager =(Connectivitymanager) Context.getsystemservice (context.connectivity_service); theNetworkinfo Mmobilenetworkinfo =Mconnectivitymanager.getnetworkinfo (connectivitymanager.type_mobile);Bayi             if(Mmobilenetworkinfo! =NULL) { the                 returnmmobilenetworkinfo.isavailable (); the             } -         } -         return false; the     } the  the     /** the * Gets the type information of the current network connection -      *  the      * @paramContext the      * @return the      */94      Public Static intGetconnectedtype (Context context) { the          the         if(Context! =NULL) { theConnectivitymanager Mconnectivitymanager =(Connectivitymanager) Context.getsystemservice (context.connectivity_service);98Networkinfo Mnetworkinfo =mconnectivitymanager.getactivenetworkinfo (); About             if(Mnetworkinfo! =NULL&&mnetworkinfo.isavailable ()) { -                 returnMnetworkinfo.gettype ();101             }102         }103         return-1;104     } the 106     /**107 * Get current network status-1: No network 1:wifi network 2:wap network 3:net Network108      * 109      * @paramContext the      * @return111      */ the      Public StaticNetType Getapntype (context context) {113          theConnectivitymanager connmgr =(Connectivitymanager) Context.getsystemservice (context.connectivity_service); theNetworkinfo Networkinfo =connmgr.getactivenetworkinfo (); the         if(Networkinfo = =NULL) {117             returnnettype.nonenet;118         }119         intNType =Networkinfo.gettype (); - 121         if(NType = =connectivitymanager.type_mobile) {122             if(Networkinfo.getextrainfo (). toLowerCase (). Equals ("Cmnet")) {123                 returnnettype.cmnet;124}Else { the                 returnNettype.cmwap;126             }127}Else if(NType = =Connectivitymanager.type_wifi) { -             returnNettype.wifi;129         } the         returnnettype.nonenet;131  the     }133}

Android development tool Class 18_networkutil

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.