1

i need to check internet connection with in fraction of seconds. currently i am using this code for checking internet connection but it is taking time to check internet connection. here i am using http request and response, i send one request with some website like google, after that only i got response. is it possible to get the connection while sending request. i need to check within seconds please help me

try {
            if (currentapiVersion >= android.os.Build.VERSION_CODES.GINGERBREAD){
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
                }

            HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
            urlc.setRequestProperty("User-Agent", "Test");
            urlc.setRequestProperty("Connection", "close");
            urlc.setConnectTimeout(1500); 
            urlc.connect();
            if(urlc.getResponseCode() == 200)}  catch (IOException e) {
            Log.e(TAG, "Error checking internet connection", e);
        }

thankyou

2 Answers2

0
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

Try this -

    private ConnectivityManager conMgr;
    private NetworkInfo activeNetwork;

    conMgr =  (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        activeNetwork = conMgr.getActiveNetworkInfo();
        if (activeNetwork != null && activeNetwork.isConnected()) {
// Do your task 
}
yuva ツ
  • 3,707
  • 9
  • 50
  • 78
  • hi previously i have used this method only isNetworkAvailable(), i think this is for only network checking i need to check internet connection – user3728511 Oct 15 '14 at 07:02
  • No. Its also checks internet connection is available or not.its working in my code.Try it.. – yuva ツ Oct 15 '14 at 07:05
  • ok if it is normal internet as well as wifi, is it working or not? – user3728511 Oct 15 '14 at 07:17
  • Yes its working for both..I'm using it in both condition data plan and wi-fi.I think you haven't tried it.Be sure before commenting from next time – yuva ツ Oct 15 '14 at 07:21
  • hi i tried your code. i have error return type for the method is missing. could you please tell me what is that error – user3728511 Oct 15 '14 at 07:47
  • Post complete code and logcat also – yuva ツ Oct 15 '14 at 09:00
  • hi it is working thank you so much but i have doubt. if internet sometimes get slow, at the time what will happen about checking internet connection – user3728511 Oct 15 '14 at 09:04
  • I think in that case you will get time out exception..Upvote and accept my answer as its working now... – yuva ツ Oct 15 '14 at 09:07
  • could you please tell me what is get time out exception how can i implement that in this function – user3728511 Oct 15 '14 at 10:05
  • In the if condition you will start your asynctask for performing network operation (Your above code).in the catch block you can handle this condition – yuva ツ Oct 15 '14 at 10:10