Network:android Network Judgment (WiFi, 3G and others)

Source: Internet
Author: User

Package Mark.zeng;      Import Java.util.List;   Import Android.content.Context;   Import Android.location.LocationManager;   Import Android.net.ConnectivityManager;   Import Android.net.NetworkInfo;      Import Android.telephony.TelephonyManager;  Public classNetworkprober {/** * Network Availability * * @param activity * @return*/        Public StaticBoolean isnetworkavailable (context context) {Connectivitymanager Connectivity=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); if(Connectivity = =NULL) {           } Else{networkinfo[] info=Connectivity.getallnetworkinfo (); if(Info! =NULL) {                    for(inti =0; i < info.length; i++) {                       if(info[i].getstate () = =NetworkInfo.State.CONNECTED) {return true; }                   }               }           }           return false; }          /** * GPS Open * * @param context * @return*/        Public StaticBoolean isgpsenabled (context context) {Locationmanager Locationmanager=((Locationmanager) context. Getsystemservice (Context.location_service)); List<String> accessibleproviders = Locationmanager.getproviders (true); returnAccessibleproviders! =NULL&& accessibleproviders.size () >0; }          /** * WiFi is open*/        Public StaticBoolean iswifienabled (context context) {Connectivitymanager Mgrconn=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); Telephonymanager Mgrtel=(Telephonymanager) context. Getsystemservice (Context.telephony_service); return(Mgrconn.getactivenetworkinfo ()! =NULL&&mgrconn. Getactivenetworkinfo (). GetState ()= = NetworkInfo.State.CONNECTED) | |Mgrtel. Getnetworktype ()==Telephonymanager.network_type_umts); }          /** * Determine if the current network is a WiFi network * if (Activenetinfo.gettype () ==connectivitymanager.type_mobile) {//judgment 3G Net * * @param context * @return Boolean*/        Public StaticBoolean Iswifi (context context) {Connectivitymanager Connectivitymanager=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); Networkinfo Activenetinfo=Connectivitymanager.getactivenetworkinfo (); if(Activenetinfo! =NULL&& Activenetinfo.gettype () = =Connectivitymanager.type_wifi) {               return true; }           return false; }          /** * determine if the current network is a 3G network * * @param context * @return Boolean*/        Public StaticBoolean is3g (context context) {Connectivitymanager Connectivitymanager=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); Networkinfo Activenetinfo=Connectivitymanager.getactivenetworkinfo (); if(Activenetinfo! =NULL&& Activenetinfo.gettype () = =connectivitymanager.type_mobile) {               return true; }           return false; There are two additional ways to determine if a network is available: Public StaticBoolean isnetworkavailable_00 (context context) {Connectivitymanager cm=((Connectivitymanager) context. Getsystemservice (Context.connectivity_service)); if(cm! =NULL) {Networkinfo info=Cm.getactivenetworkinfo (); if(Info! =NULL&&info.isconnectedorconnecting ()) {                   return true; }           }           return false; }           Public StaticBoolean isnetworkavailable_01 (context context) {Connectivitymanager cm=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); Networkinfo Network=Cm.getactivenetworkinfo (); if(Network! =NULL) {               returnnetwork.isavailable (); }           return false; A more rigorous notation: Public StaticBoolean checknet (context context) {Try{Connectivitymanager Connectivity=(Connectivitymanager) Context.getsystemservice (Context.connectivity_service); if(Connectivity! =NULL) {Networkinfo info=Connectivity.getactivenetworkinfo (); if(Info! =NULL&&info.isconnected ()) {                                          if(info.getstate () = =NetworkInfo.State.CONNECTED) {return true; }                   }               }           } Catch(Exception e) {return false; }           return false; } 

Network:android Network Judgment (WiFi, 3G and others)

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.