5

Seem like whole NetworkInfo is deprecated on API 29.

So I am looking for an alternative to check if the network is connected. E.g. alternative to

connectivityManager.activeNetworkInfo?.isConnected == true

Sidenote: I know there is a callback now, but I'd like to get this info synchronously. Also, of course I'm aware that it may not be a precise info though I'd like to have it.

Shahbaz Hashmi
  • 2,631
  • 2
  • 26
  • 49
hrach
  • 2,443
  • 2
  • 26
  • 37

5 Answers5

4

The solution is this:

val capability = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
return capability?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) ?: false
hrach
  • 2,443
  • 2
  • 26
  • 37
1

This is working on API 29:

connectivityManager.isDefaultNetworkActive
Artur Gniewowski
  • 470
  • 7
  • 17
  • It seems that this still returns true, even in flight mode. – hrach Jul 11 '19 at 08:24
  • It is possible to have an active network during Flight Mode. To quote Techopedia: `In general, features like FM receiver, Bluetooth, Wi-Fi and GPS should still be operable even when the device is in airplane mode` – Zun Jul 11 '19 at 08:50
1
android.net.NetworkInfo

This class was deprecated in API level 29. You should instead use the ConnectivityManager.NetworkCallback API to know about connectivity changes

Referrence Link https://developer.android.com/reference/android/net/NetworkInfo.html

ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();

connectivityManager.registerNetworkCallback(
                    builder.build(),
                    new ConnectivityManager.NetworkCallback() {

                        @Override
                        public void onAvailable(Network network) {
                            // Network Available
                        }


                        @Override
                        public void onLost(Network network) {
                            // Network Not Available
                        }
                    }
            );
Sabyasachi
  • 3,499
  • 2
  • 14
  • 21
0

The solution is

public void registerNetworkCallback()
{
    try {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkRequest.Builder builder = new NetworkRequest.Builder();

        connectivityManager.registerNetworkCallback(builder.build(),new ConnectivityManager.NetworkCallback() {
                    @Override
                    public void onAvailable(Network network) {
                        Variables.isNetworkConnected = true; // Global Static Variable
                    }
                    @Override
                    public void onLost(Network network) {
                        Variables.isNetworkConnected = false; // Global Static Variable
                    }
                }

        );
        Variables.isNetworkConnected = false;
    }catch (Exception e){
        Variables.isNetworkConnected = false;
    }
}

Check full code here : Gist

0
private boolean checkInternetConnection(Activity activity) {
    ConnectivityManager connectivityManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        Network network = null;
        if (connectivityManager == null) {
            return false;
        } else {
            network = connectivityManager.getActiveNetwork();
            NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(network);
            if (networkCapabilities == null) {
                return false;
            }
            if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ||
                    networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
                return true;
            }
        }
    } else {
        if (connectivityManager == null) {
            return false;
        }
        if (connectivityManager.getActiveNetworkInfo() == null) {
            return false;
        }
        return connectivityManager.getActiveNetworkInfo().isConnected();
    }
    return false;
}
Mahdi Zareei
  • 1,299
  • 11
  • 18
  • Thank you for your contribution to the community. This may be a correct answer, but it’d be useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who may not be familiar with the syntax. Further, it can help reduce the need for follow-up questions. Would you mind updating your comment with additional details? – Jeremy Caney May 12 '20 at 18:00