如何获取网络信息 - Android
How to get Network Info - Android
下面是我的代码:
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
}
当我 运行 上面的代码时,我收到 Deprecated
警告说 getActiveNetworkInfo
和 isConnected
方法已被弃用。
我不想再继续这段代码了。以上代码有哪些替代方案?
有人可以帮助我吗?提前致谢
下面是我在我的应用程序中用来检查设备是否连接到网络的代码。截至目前,它不包含任何已弃用的代码。
Kotlin
private fun isInternetAvailable(): Boolean {
var result = false
val connectivityManager = applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
connectivityManager?.let {
it.getNetworkCapabilities(connectivityManager.activeNetwork)?.apply {
result = when {
hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
else -> false
}
}
}
return result
}
Java
public static boolean isInternetAvailable(Context context) {
boolean result = false;
ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
if (networkCapabilities != null) {
if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
result = true;
} else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
result = true;
} else {
result = false;
}
}
}
return result;
}
阅读有关连接管理器和网络功能的更多信息here.
下面是我的代码:
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
}
当我 运行 上面的代码时,我收到 Deprecated
警告说 getActiveNetworkInfo
和 isConnected
方法已被弃用。
我不想再继续这段代码了。以上代码有哪些替代方案?
有人可以帮助我吗?提前致谢
下面是我在我的应用程序中用来检查设备是否连接到网络的代码。截至目前,它不包含任何已弃用的代码。
Kotlin
private fun isInternetAvailable(): Boolean {
var result = false
val connectivityManager = applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
connectivityManager?.let {
it.getNetworkCapabilities(connectivityManager.activeNetwork)?.apply {
result = when {
hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
else -> false
}
}
}
return result
}
Java
public static boolean isInternetAvailable(Context context) {
boolean result = false;
ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
if (networkCapabilities != null) {
if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
result = true;
} else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
result = true;
} else {
result = false;
}
}
}
return result;
}
阅读有关连接管理器和网络功能的更多信息here.