检查Android上是否可以访问互联网
我如何检查互联网是否有。但我发现这个代码,但当wifi连接它返回真的,但我知道,没有互联网。没有互联网接入检查Android上是否可以访问互联网,android,Android,我如何检查互联网是否有。但我发现这个代码,但当wifi连接它返回真的,但我知道,没有互联网。没有互联网接入 public boolean chechInternet_con(){ ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); android.net.NetworkInfo wifi =connec.get
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;
}
试试这个
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;
}
请参阅下面的代码
public static boolean checkConnection(Context context) {
final ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo netInfo = mConnectivityManager.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
} else
return false;
}
请参阅下面的代码
public static boolean checkConnection(Context context) {
final ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo netInfo = mConnectivityManager.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
} else
return false;
}
因此,它连接到wi-fi网络,但没有互联网接入。对吧? 尝试ping 8.8.8.8或google.com以了解是否有互联网接入。
将帮助您。因此,它已连接到wi-fi网络,但无法访问internet。对吧? 尝试ping 8.8.8.8或google.com以了解是否有互联网接入。
将对您有所帮助。您是否在设备中测试了您的代码?已回答此问题。请检查此链接您是否在设备中测试了您的代码?已回答此问题。请检查此链接您是否可以从“if”中删除此info0==连接| | info1==连接条件,然后检查?我的设备已连接到wifi,但无法访问internet,我无法进入网站:(您是否可以从“如果”中删除此info0==连接| | info1==连接条件,然后检查?我的设备已连接到wifi但没有internet访问,我无法进入网站:(谢谢,但您仅检查连接或连接,但我的设备已连接到wifi但没有internet访问。我的意思是我无法进入任何网站:(谢谢,但您只检查了已连接或正在连接,但我的设备已连接到wifi,但无法访问互联网。我的意思是我无法进入任何网站:(