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