如何在Android中检查任何类型的网络状态?
答案 0 :(得分:4)
if (Context.getSystemService(Context.CONNECTIVITY_SERVICE).getNetworkInfo(
ConnectivityManager.TYPE_MOBILE)
|| Context.getSystemService(Context.CONNECTIVITY_SERVICE).getNetworkInfo(
ConnectivityManager.TYPE_WIFI) {
// do something
}
答案 1 :(得分:1)
为了检查网络,此功能可以帮助您。
`public boolean InternetConnection()
{
ConnectivityManager connectivity = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}`
如果网络在那里,它将返回true,否则它将返回false。
答案 2 :(得分:0)
可以检查网络可用性
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();