Jason W
Jason W

Reputation: 117

Check For Internet Connectivity Causes Android Not Responding Error

This is my first app, so I'm still learning. I created a class (Check Network) that I call on to check for internet connectivity before I display an ad. The method in the class returns a boolean, here is the first method - a test connection to Google:

public boolean isConnectedToGoogle() {

        Runtime runtime = Runtime.getRuntime();

        boolean reachGoogle = false;

        try {
            Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
            int exitValue = ipProcess.waitFor();
           
            if(exitValue == 0) {
               
                reachGoogle = true;

            } else {
           
                reachGoogle = false;
            }

        } catch (IOException e) {
            //e.printStackTrace();
            reachGoogle = false;
        } catch (InterruptedException e) {
            //e.printStackTrace();
            reachGoogle = false;
        }


        return reachGoogle;
    }

Next, I use the above method in the method below to test the network, and return true or false:

 public boolean isNetworkNull(Context context) {

        boolean isNetWorkNull;

        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

        if(activeNetwork != null) {
                                       
            if(isConnectedToGoogle()) {
       
                isNetWorkNull = false;

            } else {
         
                isNetWorkNull = true;
            }


        } else {
          
            isNetWorkNull = true;
        }


        return isNetWorkNull;
    }

So, throughout my app I use the code to test the connection:

boolean isNetworkNull = checkNetwork.isNetworkNull(getApplicationContext());

if(isNetworkNull) {
  // do this
} else {
  // do that
}

In my Google Play Console I've discovered numerous "Android Not Responding" events due to the isConnectedToGoogle() method. The console states:

nameOfApp.CheckNetwork.isConnectedToGoogle

input dispatching time out

The best I can understand is the call to Google is taking too long (maybe the phone has a weak internet connection) and thus it times out and the app records an android not responding error.

If this is accurate, is there a way to say "listen" and if no response received from Google after time X have the isConnectedToGoogle skip the check and just return a true or false? Or is there a better approach to this? Any assistance would be greatly appreciated.

Upvotes: 0

Views: 185

Answers (1)

Jason W
Jason W

Reputation: 117

I think I found the answer after doing a bit more research. I changed the code as such for the isConnectedToGoogle() Method:

Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
            // int exitValue = ipProcess.waitFor(); // original
            boolean wasGoogleReached = ipProcess.waitFor(1000, TimeUnit.MILLISECONDS);

            if(wasGoogleReached) {

                reachGoogle = true;

            } else {

                reachGoogle = false;
            }

I believe this should handle the time out. This link also helped.

Upvotes: 0

Related Questions