0

I have to handle the following peculiar scenario:

  • My phone and hence the app is connected to a Wifi Router.
  • Now, the ethernet cable to the router is taken out so the internet connectivity is lost.
  • But the phone is still connected to this Wifi Router.

Is there a broadcast or callback that i could get when the above scenario occurs?

Sunny
  • 7,444
  • 22
  • 63
  • 104

1 Answers1

0

Most simplest and easy way is: ping to any server like here. I ping to google, check whether a response is empty or no ;) ... This function call within android activity, when you have a doubt regard connectivity.

public void GET(){

    String result = "";
    HttpResponse response;
    HttpEntity entity;

    try {

        HttpPost httppost = new HttpPost("http://google.com");
        DefaultHttpClient httpclient = getHttpClientImpl();

        response = httpclient.execute(httppost);
        entity = response.getEntity();

        if (entity != null) {
            InputStream is = entity.getContent();
            if (is != null) {
                StringBuilder sb = new StringBuilder();
                String line;
                try {
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(is, "UTF-8"), 8 * 1024);
                    while ((line = reader.readLine()) != null) {
                        sb.append(line);// .append("\n");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    is.close();
                }
                result = sb.toString();
                if (responseXML.equals("")){

                //  Internet connectivity is lost.

                }else {

                //  Internet connectivity still here xnjoy.

                }

            }
            entity.consumeContent();
        }
        httpclient.getConnectionManager().shutdown();

    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }

 }
Mohsin
  • 1,586
  • 1
  • 22
  • 44