Home » Android » Android, how to override the internet connectivity check when connecting to wifi network?

Android, how to override the internet connectivity check when connecting to wifi network?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’ve noticed on Marshmallow (e.g. Nexus 6P) and also on some more recently updated Lollipop phones (e.g. Galaxy S5) that when I connect to a wifi network that has no internet, the network will not fully connect until the user accepts the prompt stating that the network has no internet access.

Is there any way to programmatically get around this check and allow wifi connections to proceed regardless of internet access?

How to&Answers:

Didn’t try these but you might try redirecting www.google.com and 8.8.8.8 to 127.0.0.1 (you can use iptables or modify /system/hosts). You can also try digging AOSP source code (these are the only isReachable() checks I found in AOSP).

If you decide to lookup AOSP, you can start here

P.S. You’ll need root for iptables and /system/hosts manipulation.

Answer:

You may try this

To verify network availability:

private Boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
}

To verify internet access(connected wifi is active or not):

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;
}

Answer:

Check these Link..

check Internet connection

public final boolean isInternetOn()
{
  ConnectivityManager connec = (ConnectivityManager)
    getSystemService(Context.CONNECTIVITY_SERVICE);

  // ARE WE CONNECTED TO THE NET
  if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
       connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED )
  {
    // MESSAGE TO SCREEN FOR TESTING (IF REQ)
    //Toast.makeText(this, connectionType + ” connected”, Toast.LENGTH_SHORT).show();
    return true;
  }
  else if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
    ||  connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED  )
  {
    return false;
  }

  return false;
  }

check Internet connection with url if available

public static boolean hasActiveInternetConnection(Context context) {
    if (isNetworkAvailable(context)) {
        try {
            HttpURLConnection urlc = (HttpURLConnection) (new URL("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!");
    }
    return false;
}