0

I have been using the following code (acquired from the web) to check for Internet connection in my Android (web) app.

import android.content.Context;
import android.net.ConnectivityManager;

public class DetectConnection {
    public static boolean checkInternetConnection(Context context) {

        ConnectivityManager con_manager = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);

        return (con_manager.getActiveNetworkInfo() != null
                && con_manager.getActiveNetworkInfo().isAvailable()
                && con_manager.getActiveNetworkInfo().isConnected());
    }
}

Android studio is now giving me deprecation warnings concerning the use of getActiveNetworkInfo(). A simple search in the Internet shows that I should be using the API in [1]. I however could not figure out how to do it. Any ideas?

[1] https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback

2 Answers2

0
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;

public class NetworkCheck {
    private boolean connected = false;

    public NetworkCheck(Context context) {
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

            connectivityManager.registerDefaultNetworkCallback(
                new ConnectivityManager.NetworkCallback() {
                    @Override
                    public void onAvailable(Network network) {
                        connected = true;
                    }

                    @Override
                    public void onLost(Network network) {
                        connected = false;
                    }
                }

            );
        } catch (Exception e) {
            connected = false;
        }
    }

    public boolean isConnected() {
        return connected;
    }
}

You can check something like this

  • Thanks a lot fort the prompt answer. Is there anyway this can be changed to support API level 21? The call "registerDefaultNetworkCallback" requires API level 24 according to my Android Studio. I know I am still targeting a tiny portion of ancient devices... –  Jul 11 '22 at 20:59
0

Try This!

boolean connected = false;
ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED || 
            connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
        
        connected = true;
    }
    else
        connected = false;

You will need this permission in your manifest:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Harshil
  • 162
  • 11
  • Thanks. The following constructs used in your code {NetworkInfo, TYPE_MOBILE, getNetworkInfo(int), getState(), State, CONNECTED, TYPE_WIFI} have all been deprecated. So this is does not answer my question. –  Jul 13 '22 at 10:47
  • I am using latest version and it's working there is not deprecated function https://prnt.sc/uqTf2txHMync – Harshil Jul 13 '22 at 11:38
  • If you add tasks.withType(JavaCompile) {options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"} inside "allprojects" to the project's build.gradle, you get deprecation warnings. –  Jul 13 '22 at 12:03