gpt4 book ai didi

android - 检查 Android 上是否可以访问互联网

转载 作者:行者123 更新时间:2023-11-30 04:06:01 25 4
gpt4 key购买 nike

我如何检查互联网是否有。但是我找到了这段代码,但是当 wifi 连接时它返回 true,但我知道没有互联网。无法访问互联网。

 public boolean chechInternet_con(){
ConnectivityManager connec = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi =connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

// Here if condition check for wifi and mobile network is available or not.
// If anyone of them is available or connected then it will return true, otherwise false;

if (wifi.isConnected() || mobile.isConnected()) {
return true;
}
return false;

}

最佳答案

试试这个,

public boolean isInternetOn(Context ctx) {
this.mContext = ctx;
ConnectivityManager Connect_Manager = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
State connected = NetworkInfo.State.CONNECTED;
State connecting = NetworkInfo.State.CONNECTING;
State disconnected = NetworkInfo.State.DISCONNECTED;

State info0 = Connect_Manager.getNetworkInfo(0).getState();
State info1 = Connect_Manager.getNetworkInfo(1).getState();

// ARE WE CONNECTED TO THE NET
if (info0 == connected || info0 == connecting || info1 == connecting
|| info1 == connected) {

Log.d("Internet", "Connected");
return true;
} else if (info0 == disconnected || info1 == disconnected) {
Log.d("Internet", "DisConnected");
return false;
}
return false;
}

关于android - 检查 Android 上是否可以访问互联网,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11662707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com