0

Before answer or taking any action just by seeing the question title please read description carefully, I am trying to do this yesterday and today a full day and night but I couldn't find any acceptable solution. Some are deprecated from android some are not working, some are just checking internet is just connected or not. But that's not what I am asking for.

I have applied many Stack Overflow answers in this regard but nothing is working perfectly. The functionality I am asking for is, say MathActivity.java and activity_math.xml is a page loaded after clicking a button from mainActivity.java. When MathActivity.java is starting after clicking the button its primarily can check active internet connection via the ping method. Though its not working properly,

public boolean checkIntCON() {
        try {
            Process ipProcess = Runtime.getRuntime().exec("/system/bin/ping -c 1 8.8.8.8");
            return (ipProcess.waitFor() == 0);
        }
        catch (IOException e)          { e.printStackTrace(); }
        catch (InterruptedException e) { e.printStackTrace(); }
        return false;
    }

But its check just one time when I am entering [I did that work but I don't want that], say user is reading a pdf in activity_math.xml and when user reading this user turned off internet connection, I am also able to listen that functionality when user turn off connection this would be ok but if user again turn on data with internet unavailability then I can't listen this .

I want to get recent and maximum device supported solution to check in continues internet connection. say user is reading a pdf, after five minutes I want to check if the user have an active internet connection or not. Connection should be must transferrable internet connection. I have found AsyncTask solution but its deprecated. The main focus is I want to check user active internet connection after a certain interval.

Here is an image to explain more, See internet is connected but not active internet but its showing that data is connected

Is there any solution? If any please try to give me in detail code that i just want to copy and paste in project. I have visited all in stack overflow again I am mentioning.

Md. Moniruzzaman
  • 637
  • 6
  • 21
  • Does this answer your question? [Detect whether there is an Internet connection available on Android](https://stackoverflow.com/questions/4238921/detect-whether-there-is-an-internet-connection-available-on-android) – Eyosiyas Oct 09 '20 at 10:42
  • this is just showing status, i mean internet is connected or not. Nut not checking the connected internet is usable or not – Md. Moniruzzaman Oct 09 '20 at 10:45
  • Basically if you want to know if you can successfully execute a given web request, then the only 100% reliable way to know for sure is to attempt to do that web request. It's quite possible for everything to be "detected as connected" and some subset of the internet still being blocked of (such as by a work/school firewall). No general "device is connected" status will ever be granular enough to tell you *specifically* what will work or not. – Joachim Sauer Oct 09 '20 at 10:47
  • What is the difference between the internet being "connected" vs "available"? What about the solution you gave is "not working properly"? – Ryan M Oct 09 '20 at 22:44
  • say you have no data balance, or no balance in your sim card. or Sometimes you are connected to a WIFI but there is no internet then this all logic is showing true, But i need to execute false when its connected but there is no transferable internet available, Look at this photo. Cellular data is connected but there is a ? sign in left up corner that means there is no available internet! @Ryan M – Md. Moniruzzaman Oct 15 '20 at 15:38

1 Answers1

2

Pass context and call the function, I think this is what you are looking for.

 public static boolean isConnected(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
            Log.d("VIP", "isConnected: " + capabilities.getTransportInfo() + " down stream " + capabilities.getLinkDownstreamBandwidthKbps() +
                    " up stream " + capabilities.getLinkUpstreamBandwidthKbps() + " Cellular " + capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
                    + " WiFi " + capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) + " Ethernet " + capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET));
            return capabilities != null && capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET);
        } else {
            NetworkInfo networkInfo = Objects.requireNonNull(connectivityManager).getActiveNetworkInfo();
            boolean b = networkInfo != null && networkInfo.isConnected() && networkInfo.getDetailedState() != NetworkInfo.DetailedState.VERIFYING_POOR_LINK;
            Log.d("VIP", "isConnected: else is connected " + b);
            return b;
        }
    }
Eyosiyas
  • 1,422
  • 1
  • 9
  • 25
  • how do you call this? This is stopping the activity, not working i have passed context via it. this context is from Context context. – Md. Moniruzzaman Oct 09 '20 at 10:12
  • Add these into your manifest – Eyosiyas Oct 09 '20 at 10:16
  • also added still keep stopping app – Md. Moniruzzaman Oct 09 '20 at 10:18
  • @MoniruzzamanMonir: if you say "stopping app", then take a look at the logcat output to see **why** it's crashing. Android apps don't usually just "silently go away". – Joachim Sauer Oct 09 '20 at 10:20
  • Attempt to invoke virtual method 'java.lang.Object this it the logcat error – Md. Moniruzzaman Oct 09 '20 at 10:23
  • @Joachim Sauer if i get solution after self researching of two days then why I post it here? – Md. Moniruzzaman Oct 09 '20 at 10:42
  • @MoniruzzamanMonir: what is the stack trace? what is the full exception message? That's the kind of information to be able to tell you what is going wrong! If you find something in logcat that doesn't help you but seems related, then update your question to include it. It's useful information! – Joachim Sauer Oct 09 '20 at 10:45
  • @JoachimSauer I have resolve keep stoping app. But the [problem is Eyosiyas Berketab answer is just showing the status, this is connected or not. Not cheking the connected internet is usable or not – Md. Moniruzzaman Oct 09 '20 at 10:49