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