Skip to content Skip to sidebar Skip to footer

How To Check That Internet On Local Wifi Is Available Or Not?

I need to check oin my app that internet on local wifi is available or not , i tried requestRouteToHost but it always returning false , so please help

Solution 1:

Here is the best way to test if the device has INTERNET connection period.

publicstaticbooleanhasActiveInternetConnection(Context context) {
if (isNetworkAvailable(context)) {
    try {
        HttpURLConnectionurlc= (HttpURLConnection) (newURL("http://www.google.com").openConnection());
        urlc.setRequestProperty("User-Agent", "Test");
        urlc.setRequestProperty("Connection", "close");
        urlc.setConnectTimeout(1500); 
        urlc.connect();
        return (urlc.getResponseCode() == 200);
    } catch (IOException e) {
        Log.e(LOG_TAG, "Error checking internet connection", e);
    }
} else {
    Log.d(LOG_TAG, "No network available!");
}
returnfalse;

}

Here's a way to check if it has wifi connection or data connection.

privatebooleancheckInternetConnection()
  {

    ConnectivityManagerconMgr= (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);

    // ARE WE CONNECTED TO THE NETif (conMgr.getActiveNetworkInfo() != null

            && conMgr.getActiveNetworkInfo().isAvailable()

            && conMgr.getActiveNetworkInfo().isConnected()) 
    {

    returntrue;

    }
    else 
    {
    returnfalse;

    }
}

Post a Comment for "How To Check That Internet On Local Wifi Is Available Or Not?"