/* Add permissions */
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE" ></uses-permission>
/* Add and invoke the following methods where the network needs to be judged */
Public Booleanisnetworkavilable (Context context) {Connectivitymanager Connectivitymanager=(Connectivitymanager) Context.getsystemservice (Context.connectivity_service); if(Connectivitymanager = =NULL){ return false; }Else{networkinfo Networkinfo=Connectivitymanager.getactivenetworkinfo (); if(Networkinfo! =NULL ){ /*determine if the network is connected*/ if(networkinfo.getstate () = =NetworkInfo.State.CONNECTED) {/*determine if the WiFi connection is*/ if(Networkinfo.gettype () = =Connectivitymanager.type_wifi) {Toast.maketext (Getapplicationcontext (),"WiFi", Toast.length_short). Show (); } /*determine if the data connection is*/ Else if(Networkinfo.gettype () = =connectivitymanager.type_mobile) {Toast.maketext (Getapplicationcontext (),"GPRS", Toast.length_short). Show (); } return true; } } } return false; }
If data is connected through Telephonymanager to obtain data is 3g/4g, unicom or mobile telecommunications and other information
/* Get service information from your phone's suppliers * *
Telephonymanager Telephonymanager = (telephonymanager) context.getsystemservice (Context.telephony_service);
Get phone format: int getphonetype ()
int PHONE_TYPE_CDMA mobile phone format for CDMA, telecom
int PHONE_TYPE_GSM mobile phone format for GSM, Mobile and Unicom
int phone_type_none cell phone format unknown
Android gets and determines network connection status