1

I have using a function to successfully detect internet connectivity in Android 4.1 to 6.0 apps, by sending a ping. (Because, remember that being connected to WiFi or the mobile network doesn't guarantee by itself, that device has actual access to the internet.) My problem is that I discovered that for some reason, my code is not working in Android 4.3. Do you know how to fix this? or perhaps, do you know a better method to detect internet connectivity on all devices within Android versions 4.1 - 6.0?

    //this function works perfectly!! except in Android 4.3 Jellybean
    public boolean isOnline() {
            Runtime runtime = Runtime.getRuntime();
            try {
                Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
                int     exitValue = ipProcess.waitFor();
                if(exitValue==0)
                    app.logy("PING @K - ONLINE!!");
                return (exitValue == 0);
            } catch (IOException e){
                e.printStackTrace();
                app.logy("PING ERROR - OFFLINE");
                return false;
                }
            catch (InterruptedException e){
                e.printStackTrace();
                app.logy("PING ERROR - OFFLINE");
                return false;
            }
        }
Josh
  • 6,251
  • 2
  • 46
  • 73

1 Answers1

1

http://clients3.google.com/generate_204 is used to check connection, pings works differently in diferent devices.

Why does ping works on some devices and not others?

private int inter = 0;   





            class checkconne extends AsyncTask<String, String, String> {

                        @Override
                        protected void onPreExecute() {
                            super.onPreExecute();



                        }
                        @Override
                        protected String doInBackground(String... args) {

                            int kk=0;
                            try {
                                HttpURLConnection urlc = (HttpURLConnection)
                                        (new URL("http://clients3.google.com/generate_204")
                                                .openConnection());
                                urlc.setRequestProperty("User-Agent", "Android");
                                urlc.setRequestProperty("Connection", "close");
                                urlc.setConnectTimeout(1500);
                                urlc.connect();
                                kk= urlc.getResponseCode();
                            } catch (IOException e) {
                                Log.e("qweqwe", "Error checking internet connection", e);
                            }

                            inter=kk;



                            return null;
                        }
                        @Override
                        protected void onPostExecute(String file_url) {


                            if (inter == 204){       
             Toast.makeText(MainActivity3.this, "is connected", Toast.LENGTH_LONG).show();             

                            }else{    


                                Toast.makeText(MainActivity3.this, "No connection", Toast.LENGTH_LONG).show();

                            }


                        }
                    }
Community
  • 1
  • 1
Exumer
  • 56
  • 1
  • 9