1

I need to detect whether internet connection is available and working fine in android. I need this condition check to call before i start any of my WebView Activities.

So far I have managed to implement the isAvailable() feature found in this answer: Class which check for connectivity

However, the above method fails in case when wifi is ON but ethernet plug is NOT connected. Some say using ping is a good idea. But I am not able to successfully implement this feature.

  1. How do I implement the ping feature in the code given in the link
  2. My android application uses Retrofit API for sending and receiving PHP contents. Can Retrofit help in checking for internet connectivity?
  3. Can you suggest any other simple and easy to implement method for checking Real-Time internet connectivity?

Code I have used from this link :-

  1. Class name: AppStatus

      import android.content.Context;
      import android.net.ConnectivityManager;
      import android.net.NetworkInfo;
      import android.util.Log;
      public class AppStatus {
    
      private static AppStatus instance = new AppStatus();
      static Context context;
      ConnectivityManager connectivityManager;
      NetworkInfo wifiInfo, mobileInfo;
      boolean connected = false;
    
      public static AppStatus getInstance(Context ctx) {
              context = ctx.getApplicationContext();
              return instance;
          }
    
          public boolean isOnline() {
    try {
        connectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
    
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    connected = networkInfo != null && networkInfo.isAvailable() &&
            networkInfo.isConnected();
    return connected;
    
    
    } catch (Exception e) {
        System.out.println("CheckConnectivity Exception: " + e.getMessage());
        Log.v("connectivity", e.toString());
    }
    return connected;
    

    }

  2. This code helps in checking for internet connectivity but fails in the previously mentioned condition :-

    if (AppStatus.getInstance(this).isOnline()) {
        Toast.makeText(this,"You are online!!!!",8000).show();
    } else {
        Toast.makeText(this,"Internet Connection is required !",8000).show();
        Log.v("Home", "############################You are not online!!!!");
        finish();
    }
    
Community
  • 1
  • 1
Hemang
  • 121
  • 1
  • 5
  • 2
    Possible duplicate of [How to check internet access on Android? InetAddress never timeouts...](http://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-timeouts) – camelCaseCoder Apr 20 '16 at 09:34
  • 1
    It does seem to have the PING code i need. Ill try to integrate it with my code and see. – Hemang Apr 20 '16 at 10:26
  • Thanks @camelCaseCoder . I integrated the code i found in the link you provided. I have also added it in my answer. – Hemang Apr 20 '16 at 10:32

2 Answers2

0
public static boolean isNetworkAvailable(Context mContext) {

    ConnectivityManager cm = (ConnectivityManager) mContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        Log.e("Network Testing", "***Available***");
        return true;
    }
    Log.e("Network Testing", "***Not Available***");
    return false;
}
0

Thanks to @camelCaseCoder 's comment. I got a code which i succesfully implemented and I would like to share it with you all and hence I am answering my own question.

  1. AppStatus becomes (after commenting out the previous code) :-

            public class AppStatus {
    
            private static AppStatus instance = new AppStatus();
            static Context context;
            //ConnectivityManager connectivityManager;
            //NetworkInfo wifiInfo, mobileInfo;
            //boolean connected = false;
    
            public static AppStatus getInstance(Context ctx) {
                context = ctx.getApplicationContext();
                return instance;
            }
    
            public boolean isOnline() {
                /*try {
        connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
    
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        connected = networkInfo != null && networkInfo.isAvailable() &&
                networkInfo.isConnected();
        return connected;
    
    
    } catch (Exception e) {
        System.out.println("CheckConnectivity Exception: " + e.getMessage());
        Log.v("connectivity", e.toString());
    }
    return connected;
    */
    Runtime runtime = Runtime.getRuntime();
    try {
    
        Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
        int     exitValue = ipProcess.waitFor();
        return (exitValue == 0);
    
    } catch (IOException e)          { e.printStackTrace(); }
    catch (InterruptedException e) { e.printStackTrace(); }
    
    return false;
    

    }

}

  1. It is implemented similarly as before:-

    //check for internet connectivity by calling isOnline-using Ping @google
    if (AppStatus.getInstance(this).isOnline()) {
        Toast.makeText(this, "You are online!!!!", 8000).show();
    } else {
        Toast.makeText(this,"Internet Connection is required !",8000).show();
        Log.v("Home", "############################You are not online!!!!");
        finish();
    }
    
Hemang
  • 121
  • 1
  • 5