0

I want to be able to get a user internet connection. I have tried

boolean connected = false;
    try {
        ConnectivityManager cm = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo nInfo = cm.getActiveNetworkInfo();
        connected = nInfo != null && nInfo.isAvailable() && nInfo.isConnected();
        return connected;
    } catch (Exception e) {
        Log.e("Connectivity Exception", e.getMessage());
    }
    return connected;

But this only gets mobile data and wifi connection status, if the user has an expired data plan or doesn't even have any data plan I will not be able to know. I want to know the internet status before making a server call instead of making a server and running in a runout time error.

Please help I have checked stack overflow, but I keep getting the same answer.

m.reiter
  • 1,796
  • 2
  • 11
  • 31
Yo Daniel
  • 114
  • 11
  • 2
    The only way your going to be able to do that is to ping a server. Until you actually try to send/receive data your not going to know the speed/stability of a connection – DevWithZachary Sep 14 '21 at 10:35
  • Thanks I lookup pinging I think it will be able to slove my problem. – Yo Daniel Sep 14 '21 at 10:39
  • kindly Find This Solution, its about pinging, https://medium.com/dsc-alexandria/implementing-internet-connectivity-checker-in-android-apps-bf28230c4e86 – Islam Alshnawey Sep 14 '21 at 10:48

1 Answers1

0

check internet coonnection with this function:

Boolean hasInternet(Context context) {
    ConnectivityManager connectivityManager =
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

        Network activeNetwork = connectivityManager.getActiveNetwork();
        NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(activeNetwork);

        if (capabilities != null) {
            if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) return true;
            else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) return true;
            else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) return true;
            else return false;
        }

    } else {
        try {
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            return networkInfo != null && networkInfo.isConnected();
        } catch (NullPointerException e) {
            return false;
        }
    }
    return false;
}
Bugs Happen
  • 2,169
  • 4
  • 33
  • 59
  • 1
    this method returns `true` when you are connected to some network (e.g. wifi or ethernet), still this network may be some intranet. besides that this is Kotlin, not Java, which op want to use (due to tag) – snachmsm Sep 14 '21 at 11:48
  • @snachmsm i'm pretty sure it is a java code snippet. ;) – programmer dreamer Mar 28 '23 at 07:12
  • @programmerdreamer I'm pretty sure that there was some other comment above mine ;) – snachmsm Mar 28 '23 at 07:52