Determine if a network connection is available in Android

Source: Internet
Author: User

First, determine whether the network connection is available

1  Public Static Booleanisnetworkavailable (Context context) {2Connectivitymanager cm =(connectivitymanager) Context3 . Getsystemservice (Context.connectivity_service); 4         if(cm = =NULL) {   5}Else {6          //if it is only used to determine the network connection7           //You can use Cm.getactivenetworkinfo (). isavailable (); 8Networkinfo[] Info =Cm.getallnetworkinfo (); 9             if(Info! =NULL) {   Ten                  for(inti = 0; i < info.length; i++) {    One                     if(info[i].getstate () = =NetworkInfo.State.CONNECTED) { A                         return true;  -                     }    -                 }    the             }    -         }    -         return false;  -}

Second, judge whether the GPS is open

1  Public Static Booleanisgpsenabled (Context context) {2Locationmanager LM =((locationmanager) Context3 . Getsystemservice (Context.location_service)); 4list<string> accessibleproviders = Lm.getproviders (true); 5         returnAccessibleproviders! =NULL&& accessibleproviders.size () > 0; 6}

Third, determine if WiFi is turned on

1  Public Static Booleaniswifienabled (Context context) {2Connectivitymanager Mgrconn =(connectivitymanager) Context3 . Getsystemservice (Context.connectivity_service); 4Telephonymanager Mgrtel =(telephonymanager) Context5 . Getsystemservice (Context.telephony_service); 6         return(Mgrconn.getactivenetworkinfo ()! =NULL&&Mgrconn7. Getactivenetworkinfo (). GetState () = = NetworkInfo.State.CONNECTED) | |Mgrtel8. Getnetworktype () = =Telephonymanager.network_type_umts); 9}

Iv. Judging whether it is a 3G network

1  Public Static Booleanis3rd (Context context) {2Connectivitymanager cm =(connectivitymanager) Context3 . Getsystemservice (Context.connectivity_service); 4Networkinfo Networkinfo =Cm.getactivenetworkinfo (); 5         if(Networkinfo! =NULL   6&& Networkinfo.gettype () = =connectivitymanager.type_mobile) {   7             return true; 8         }   9         return false; Ten}

Five, the judge is WiFi or 3g network, the embodiment of the user here, WiFi can be suggested to download or online playback.

1  Public Static BooleanIswifi (Context context) {2Connectivitymanager cm =(connectivitymanager) Context3 . Getsystemservice (Context.connectivity_service); 4Networkinfo Networkinfo =Cm.getactivenetworkinfo (); 5             if(Networkinfo! =NULL   6&& Networkinfo.gettype () = =Connectivitymanager.type_wifi) {   7                 return true; 8             }   9             return false; Ten}

Determine if a network connection is available in Android

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.