我注意到Marshmallow(例如Nexus 6P)以及一些最近更新的Lollipop手机(例如Galaxy S5),当我连接到没有互联网的wifi网络时,网络将无法完全连接,直到用户接受提示声明网络无法访问互联网。
有没有办法以编程方式绕过此检查并允许wifi连接继续进行而不管互联网访问?
答案 0 :(得分:1)
没试过这些,但您可以尝试将www.google.com
和8.8.8.8重定向到127.0.0.1(您可以使用iptables或修改/system/hosts
)。您也可以尝试挖掘AOSP源代码(这些是我在AOSP中找到的唯一的isReachable()检查。)
如果您决定查找AOSP,可以启动here
P.S。你需要root用于iptables和/ system / hosts操作。
答案 1 :(得分:1)
你可以试试这个
验证网络可用性:
private Boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
}
验证互联网访问权限(已连接的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;
}
答案 2 :(得分:0)
检查这些链接..
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;
}