R PT
R PT

Reputation: 57

How can I check internet connection in Android Q?

Before Android Q all you had to do is use the NetworkUtils class. But this class was deprecated in API 29. I've been searching for an alternative, and I couldn't seem to find one.

So, how can I check the internet connection in Android Q?

Upvotes: 5

Views: 10553

Answers (5)

Daxesh Vekariya
Daxesh Vekariya

Reputation: 581

    @IntRange(from = 0, to = 4)
public static int getConnectionType(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null) {
        return 0; // No connection
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        NetworkCapabilities capabilities = cm.getNetworkCapabilities(cm.getActiveNetwork());
        if (capabilities == null) {
            return 0; // No connection
        }
        if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
            return 2; // WiFi
        } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
            return 1; // Cellular data
        } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
            return 3; // VPN
        } else {
            return 4; // Other network types like Ethernet
        }
    } else {
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (activeNetwork == null || !activeNetwork.isConnected()) {
            return 0; // No connection
        }
        switch (activeNetwork.getType()) {
            case ConnectivityManager.TYPE_WIFI:
                return 2; // WiFi
            case ConnectivityManager.TYPE_MOBILE:
                return 1; // Cellular data
            case ConnectivityManager.TYPE_VPN:
                return 3; // VPN
            default:
                return 4; // Other network types like Ethernet
        }
    }
}

Permissions in AndroidManifest.xml

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

Upvotes: 8

Alfred Faltiska
Alfred Faltiska

Reputation: 121

Some answers recommend just checking if getNetworkCapabilities returns a non null value but if you want to check if that network could actually provide Internet, you should do this instead:

public boolean isNetworkAvailable() {
    try {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        return cm.getNetworkCapabilities(cm.getActiveNetwork()).hasCapability(NET_CAPABILITY_INTERNET);
    } catch (Exception e) {
        return false;
    }
}

And you must still have this permission:

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

If you want to check if the available Internet connection actually works (or worked when last checked), use the NET_CAPABILITY_VALIDATED capability.

Upvotes: 3

I_4m_Z3r0
I_4m_Z3r0

Reputation: 1080

Just use this code and call isInternatAvailable(context).

    private static final String CMD_PING_GOOGLE = "ping -w -c 1 google.com";

    public static boolean isInternetAvailable(@NonNull Context context) {
        return isConnected(context) && checkInternetPingGoogle();
    }

    public static boolean isConnected(@NonNull Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if(cm != null) {
            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
            return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
        } else {
            return false;
        }
    }

    public static boolean checkInternetPingGoogle(){
        try {
            int a = Runtime.getRuntime().exec(CMD_PING_GOOGLE).waitFor();
            return a == 0x0;
        } catch (IOException ioE){
            EMaxLogger.onException(TAG, ioE);
        } catch (InterruptedException iE){
            EMaxLogger.onException(TAG, iE);
        }
        return false;
    }

Upvotes: 0

Atakan Yıldırım
Atakan Yıldırım

Reputation: 882

If you are looking for a simple code, you can use this:

public static boolean isInternetConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return cm.getActiveNetwork() != null && cm.getNetworkCapabilities(cm.getActiveNetwork()) != null;
    } else {
        return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnectedOrConnecting();
    }
}

Upvotes: 8

guenter47
guenter47

Reputation: 594

The ConnectivityManager only checks whether the smartphone could theoretically establish an Internet connection. Whether an Internet connection actually exists, e.g. if the network quality is very poor, can only be determined with a ping or a web address.

Upvotes: 1

Related Questions