0

I tried below code to check the connectivity:

public static NetworkInfo getNetworkInfo(Context context) {
    if (context == null)
        return null;
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null)
        return null;
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.isConnected())
        return activeNetwork;
    else {
        for (Network n: cm.getAllNetworks()) {
            NetworkInfo nInfo = cm.getNetworkInfo(n);
            if(nInfo != null && nInfo.isConnected())
                return nInfo;
        }
    }
    return activeNetwork;
}

public static boolean isConnectivityAllowed(Context context) {
    NetworkInfo info = NetworkUtils.getNetworkInfo(context);
    return info != null && info.isConnected();
}

Generally, it works fine but in some conditions, it returns disconnected although I have a connection. After searching, testing, checking the logs, I understood when the device's battery is low if I run the app, the function returns disconnected because the OS put the system on power saver and then if I change the connectivity, the app gets the right answer. More information is available here in @phil 's answer.

Does anyone know how to check the connection when power saver in on?!

1 Answers1

0

Add the following permissions in the AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

Create a NetworkUtils class for connectivity check

public class NetworkUtils {

/**
 * Check if mobile data is enabled
 *
 * @return mobile data on or off
 */
private static boolean isMobileDataEnabled(Context context) {
    Context ctx = context.getApplicationContext();
    ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    try {
        return (Boolean) cm.getClass().getMethod("getMobileDataEnabled").invoke(cm, new Object[0]);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
    return false;
}

/**
 * Check if connected to wifi network
 *
 * @return wifi connected or not
 */
private static boolean isWifiConnected(Context context) {
    WifiManager wm = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    return wm != null && wm.isWifiEnabled() && wm.getConnectionInfo().getNetworkId() != -1;
}

/**
 * Check if mobile data is on or device is connected to wifi network
 */
public static boolean isConnected(Context context) {
    return isMobileDataEnabled(context) || isWifiConnected(context);
}}

Call NetworkUtils.isConnected() for usage

public class MainActivity extends AppCompatActivity {

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Check connectivity
    boolean isConnected = NetworkUtils.isConnected(this);
}}
ampfarisaho
  • 111
  • 1