如何在android中连接wifi网络的情况下检查Internet是否处于活动状态 [英] How to check if Internet is active with wifi network connected in android
本文介绍了如何在android中连接wifi网络的情况下检查Internet是否处于活动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在android中连接的WiFi网络上自动检查互联网是否处于活动状态?我可以检查是否已启用 wifi 或是否已连接 wifi 网络,但我不确定如何检查互联网是否已连接?这可能吗?
How to check automatically if internet is active on the WiFi network connected in android? I can check if wifi is enabled or if wifi network is connected but I am not sure how to check if internet is connected? Is this possible?
private boolean connectionAvailable()
{
boolean connected = false;
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
//we are connected to a network
connected = true;
}
return connected;
}
以上检查是否启用 wifi/wifi 连接但未连接互联网.
Above checks on wifi enabled/wifi connected or not but not internet.
推荐答案
public boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
这篇关于如何在android中连接wifi网络的情况下检查Internet是否处于活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文