2

I'm able to detect Internet connection using

public class InternetDetector {

    private Context _context;

    public InternetDetector(Context context) {
        this._context = context;
    }

    /**
     * Checking for all possible internet providers
     **/
    public Boolean isConnectingToInternet() {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
    }
    public Boolean isOnline() {
        try {
            Process p1 = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com");
            int returnVal = p1.waitFor();
            boolean reachable = (returnVal == 0);
            return reachable;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }
}

This works perfectly if there is internet, but in some cases I am able to connect to WIFI but no internet. In this case If internet is not available I nned to show a message[No INTERNET CHECK YOUR CONNECTION] to user after 10 seconds

How can I achieve it.

Suraj Makhija
  • 1,368
  • 6
  • 16
Andi
  • 119
  • 1
  • 7

3 Answers3

0

You can achieve this by using handler

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        // This method will be executed once the timer is over
        // Your message here
    }
}, 10000); //Your time here in miliseconds

After the time is completed the message will be displayed to user. Simple is that.

For 10 seconds you can add 10000

Robin Kanters
  • 4,407
  • 2
  • 18
  • 35
Abdul Kawee
  • 2,598
  • 1
  • 12
  • 26
0

You can try out the following to check for internet connectivity and therefafter show a toast in case there is no internet :

boolean isInternetAvailable = checkInternetConnection(this);

    if(!isInternetAvailable) {
        new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(this, "No INTERNET CHECK YOUR CONNECTION", Toast.LENGTH_SHORT).show();
    }
}, 10000);
    }

    public static boolean checkInternetConnection(Context context) {
        final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetworkInfo = connMgr.getActiveNetworkInfo();

        if (activeNetworkInfo != null) { // connected to the internet
            Toast.makeText(context, activeNetworkInfo.getTypeName(), Toast.LENGTH_SHORT).show();

            if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                // connected to wifi
                return isWifiInternetAvailable();
            } else if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                // connected to the mobile provider's data plan
                return true;
            }
        }
        return false;
    }

public boolean isWifiInternetAvailable() {
        try {
            InetAddress ipAddr = InetAddress.getByName("google.com"); //You can replace it with your name
            return !ipAddr.equals("");
        } catch (Exception e) {
            return false;
        }
    }
Suraj Makhija
  • 1,368
  • 6
  • 16
0

You are right, you can check if you are connected to the wifi or Cellular Network but you cannot easily check if you are really connected to the Internet or not.

Right now the only way to check if you are really connected to the internet is to connecting to a remote server! for example:

public static boolean hasInternetAccess(Context context) {
    if (isNetworkAvailable(context)) {
        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();
            return (urlc.getResponseCode() == 204 &&
                        urlc.getContentLength() == 0);
        } catch (IOException e) {
            Log.e(TAG, "Error checking internet connection", e);
        }
    } else {
        Log.d(TAG, "No network available!");
    }
    return false;
}

You can change URL to any desired address but this URL generate a determined response so you don't have to worry about server problems.. Of course you can Change it to your server to check availability of server too ^^

For more info you can check this answer: Detect if Android device has Internet connection

Community
  • 1
  • 1
Keivan Esbati
  • 2,855
  • 1
  • 17
  • 35