简体   繁体   English

Android,如何在连接到wifi网络时覆盖互联网连接检查?

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

I've noticed on Marshmallow (eg Nexus 6P) and also on some more recently updated Lollipop phones (eg 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. 我注意到Marshmallow(例如Nexus 6P)以及一些最近更新的Lollipop手机(例如Galaxy S5),当我连接到没有互联网的wifi网络时,网络将无法完全连接,直到用户接受提示说明网络没有互联网接入。

Is there any way to programmatically get around this check and allow wifi connections to proceed regardless of internet access? 有没有办法以编程方式绕过此检查并允许无线连接继续进行无线连接?

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 ). 没试过这些,但您可以尝试将www.google.com和8.8.8.8重定向到127.0.0.1(您可以使用iptables或修改/system/hosts )。 You can also try digging AOSP source code (these are the only isReachable() checks I found in AOSP). 您也可以尝试挖掘AOSP源代码(这些是我在AOSP中找到的唯一的isReachable()检查)。

If you decide to lookup AOSP, you can start here 如果您决定查找AOSP,可以从这里开始

PS You'll need root for iptables and /system/hosts manipulation. PS你需要root用于iptables和/ system / hosts操作。

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): 验证互联网访问(已连接的wifi是否处于活动状态):

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

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 如果可用,请检查与网址的Internet连接

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM