-1

I am using the following code for checking if internet connection available or not , this code works well if wifi or data disabled from mobile but problem is that this code hangs mobile when data is not receive during internet connected....

public class ConnectionDetector {

private Context _context;

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

public boolean isConnectingToInternet(){
    ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
      if (connectivity != null) 
      {
          NetworkInfo[] info = connectivity.getAllNetworkInfo();
          if (info != null) 
              for (int i = 0; i < info.length; i++) 
                  if (info[i].getState() == NetworkInfo.State.CONNECTED)
                  {
                      return true;
                  }

      }
      return false;
}

}

1 Answers1

1
  <!--Constants.INTERNET_CONNECTION_URL="YOUR_WEB_SERVICE_URL/URL OF GOOGLE";-->

    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;

    import com.vgheater.util.Constants;

    import java.io.IOException;
    import java.net.HttpURLConnection;
    import java.net.URL;

    public class CheckConnectivity {
        private Context _context;

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

        public boolean isConnectingToInternet() {
            ConnectivityManager connectivity = (ConnectivityManager) _context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivity != null) {
                NetworkInfo[] info = connectivity.getAllNetworkInfo();
                if (info != null)
                    for (int i = 0; i < info.length; i++)
                        if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                            return true;
                        }

            }
            return false;
        }

        public boolean hasActiveInternetConnection() {
            if (isConnectingToInternet()) {
                try {
                    HttpURLConnection urlc = (HttpURLConnection) (new URL(Constants.INTERNET_CONNECTION_URL).openConnection());
                    urlc.setRequestProperty("User-Agent", "Test");
                    urlc.setRequestProperty("Connection", "close");
                    urlc.setConnectTimeout(5000);
                    urlc.connect();
                    return (urlc.getResponseCode() == 200);
                } catch (IOException e) {
                    return false;
                }
            } else {
                return false;
            }
        }
    }

You can use it as follows..

private class InternetTask extends AsyncTask<String, Void, Boolean> {

      private ProgressDialog internetDialog = null;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            internetDialog = new ProgressDialog(RegisterUser.this);
            internetDialog.setCancelable(false);
            internetDialog.setCanceledOnTouchOutside(false);
            internetDialog.setMessage(Html.fromHtml("<font  color='#616161'>Checking Internet Connectivity.</font>"));
            internetDialog.show();
        }

        protected Boolean doInBackground(String... urls) {
            boolean response = false;
            try {
                response = checkConnectivity.hasActiveInternetConnection();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return response;
        }

        protected void onPostExecute(Boolean result) {
            internetDialog.dismiss();
            if (result) {
                //do stuff
            } else {
                new ShowToast(RegisterUser.this, "Connect to Internet & Try Again !!");


            }
        }

    }
Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
  • What do you mean return both statement false in hasActiveInternetConnection() ????? – Harpreet Harry Sep 28 '15 at 11:11
  • It will return true if you have internet connectivity from the line `return (urlc.getResponseCode() == 200);` – Anoop M Maddasseri Sep 28 '15 at 11:37
  • You did not understand my question properly , i want to check if in connected Internet state the data is receiving or not ..... with your code my app stuck on button when i click for post the data – Harpreet Harry Sep 28 '15 at 11:55
  • Try inside a background thread...if your connected to internet it will try to communicate with specified URL using HttpURLConnection if it will not getting any response from the particular it will return false ie, u have no data received.. – Anoop M Maddasseri Sep 28 '15 at 12:12
  • Sorry sir but this method takes too much time when data is not receiving .. i want quick check if internet data is receiving or not – Harpreet Harry Oct 09 '15 at 09:00