Copy code code as follows:
Connectivitymanager mconnectivity = (connectivitymanager) getsystemservice (Context.connectivity_service);
Telephonymanager mtelephony = (telephonymanager) this.getsystemservice (Telephony_service);
Check network connection, if no network is available, do not need to do networking operations, etc.
Networkinfo info = Mconnectivity.getactivenetworkinfo ();
if (info = null | |!mconnectivity.getbackgrounddatasetting ()) {
return false;
}
To determine the type of network connection, only some data updates are made in 3G or WiFi.
int nettype = Info.gettype ();
int netsubtype = Info.getsubtype ();
if (NetType = = Connectivitymanager.type_wifi) {
return info.isconnected ();
else if (NetType = Connectivitymanager.type_mobile
&& Netsubtype = = Telephonymanager.network_type_umts
&&!mtelephony.isnetworkroaming ()) {
return info.isconnected ();
} else {
return false;
}
Don't forget to add a check to the Androidmanifest.xml network permissions
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>