Android phone networking status, GPS

Source: Internet
Author: User

first, determine whether the network connection is available
Public Static Booleanisnetworkavailable (Context context) {Connectivitymanager cm=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); if(cm = =NULL) { } Else{//if it is only used to determine the network connection//You can use Cm.getactivenetworkinfo (). isavailable (); Networkinfo[] Info =Cm.getallnetworkinfo (); if(Info! =NULL) { for(inti = 0; i < info.length; i++) { if(info[i].getstate () = =NetworkInfo.State.CONNECTED) {return true; } } } } return false; }

Call to join <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/> This permission

second, judge whether the GPS  is open  Public Static Boolean isgpsenabled (Context context) {           = ((locationmanager) context                   . Getsystemservice ( Context.location_service));           List<String> accessibleproviders = lm.getproviders (true);            return null && accessibleproviders.size () > 0;       
Permissions to join:
<uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION" ></uses-permission>
third, determine if WiFi is turned on Public Static Booleaniswifienabled (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); } 
Iv. Judging whether it is a 3G network Public Static Booleanis3rd (Context context) {Connectivitymanager cm=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); Networkinfo Networkinfo=Cm.getactivenetworkinfo (); if(Networkinfo! =NULL&& Networkinfo.gettype () = =connectivitymanager.type_mobile) {               return true; }           return false; }  
Five, the judge is WiFi or 3g network, the embodiment of the user here, WiFi can be suggested to download or online playback.  Public Static BooleanIswifi (Context context) {Connectivitymanager cm=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); Networkinfo Networkinfo=Cm.getactivenetworkinfo (); if(Networkinfo! =NULL&& Networkinfo.gettype () = =Connectivitymanager.type_wifi) {                   return true; }               return false; }

<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>---> Mobile data rights <uses-permission Android:name= "Android.permission.ACCESS_WIFI_STATE"/>--->wifi permissions

Android phone networking status, GPS

Related Article

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.