更新时间:2023-01-01 18:57:57
我自己实现的方法:
/*
* isOnline - Check if there is a NetworkConnection
* @return boolean
*/
protected boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
return true;
} else {
return false;
}
}
请注意,这是一个 NetworkConnection-Check.如果有 NetworkConnection,它不一定是 InternetConnection.
Be aware of that this is a NetworkConnection-Check. If there is a NetworkConnection it doesn't have to be a InternetConnection.