Copy codeThe Code is as follows: ConnectivityManager mConnectivity = (ConnectivityManager) getSystemService (Context. CONNECTIVITY_SERVICE );
TelephonyManager mTelephony = (TelephonyManager) this. getSystemService (TELEPHONY_SERVICE );
// Check the network connection. If no network is available, no network connection is required.
NetworkInfo info = mConnectivity. getActiveNetworkInfo ();
If (info = null |! MConnectivity. getBackgroundDataSetting ()){
Return false;
}
// Determine the network connection type. Only data in 3G or wifi is updated.
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;
}
Check the network permissions in AndroidManifest. xml.
<Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"/>