1

I was using below method for checking weather user is connected to internet or not.

 public boolean internetIsConnected() {
        try {
            String command = "ping -c 1 google.com";
            return (Runtime.getRuntime().exec(command).waitFor() == 0);
        } catch (Exception e) {

            return false;
        }
    }

Manifest is as below

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

but after I switched to latest API 30. It just freezes the app and returns false. any suggestion to get this working.

Thank you.

Edit : As per comment's suggestions I tried below similar method but still Not working.

public boolean internetIsConnected() {
        Runtime runtime = Runtime.getRuntime();
        try {
            Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
            int exitValue = ipProcess.waitFor();
            System.out.println(" mExitValue "+exitValue);
            return (exitValue == 0);
        } catch (IOException e){
            System.out.println(" IO Error ");
            e.printStackTrace(); }
        catch (InterruptedException e) {
            System.out.println(" Interrupted Error ");
            e.printStackTrace(); }
        return false;
    }

I got :

I/System.out:  mExitValue 1

So it is failing in Try block itself. I tried finding what Exit value "1" means but it only shows for "0" Here This Happens in both Emulator as well as physical device. Any suggestion is appreciated.

Thank you

Jay
  • 11
  • 3
  • Hi Jay, welcome to StackOverflow. Since the method returns false, means `try` block failed to execute its scope, and therefore catch block executed and it returned false. So it would be useful to share the exception in your post so people can help you. You can either debug on `return false` line or add a line above to print the exception: `e` – Eren Utku Apr 29 '21 at 11:57
  • this in fact is a question about how to ping on external address/url, which is answered [here](https://stackoverflow.com/questions/3905358/how-to-ping-external-ip-from-java-android). your solution is most upvoted (well, you aren't providing full path..), but there is another, which is using "real" `Socket`, also checks `isNetworkAvailable()` as one of answers suggests. PS also read comment about possible leak in your way... – snachmsm Apr 29 '21 at 12:11
  • @ErenUtku I have updated the question and included some more info. Please Have a look. Thank you. Sorry for Late as I was busy in COVID-19 emergency duty. – Jay May 21 '21 at 07:41
  • @snachmsm I tried same method as per your suggestion But it is still not working I have updated the question. – Jay May 21 '21 at 07:42
  • I was suggesting another answer, this one with `new Socket`, posted by Alexander Mayatsky – snachmsm May 21 '21 at 07:45
  • @snachmsm ok got that, I will try it and share results. – Jay May 21 '21 at 07:55

2 Answers2

0

You can use the following function

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

public static boolean checkInternetPingGoogle(){
    try {
        int a = Runtime.getRuntime().exec(CMD_PING_GOOGLE).waitFor();
        return a == 0x0;
    } catch (IOException | InterruptedException ioE){
        Log.e("exception", ioE.toString());
    }
    return false;
}

this will return true if internet is connected or else it will return false and it is very quick as it doesn not load whole page instead it get response only.

Sadique Khan
  • 230
  • 3
  • 9
-1

You can check network availability using the following command.

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

You have to add access_network_state permission in Manifest:

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