gpt4 book ai didi

java - 将目标 sdk 更新到 29 后导入已弃用的警告

转载 作者:行者123 更新时间:2023-12-01 20:07:55 26 4
gpt4 key购买 nike

build.gradle 中将编译和 targetSdkVersion 更新为“29”后 我收到弃用警告:

warning: [deprecation] NetworkInfo in android.net has been deprecated
import android.net.NetworkInfo;
^

因此构建失败。但我使用网络信息来与 API 版本 16-29 兼容。我应该如何处理针对 api“29”及以上版本的已弃用导入?

最佳答案

我找到了这个问题的解决方案。您必须这样编写方法。

@SuppressWarnings("deprecation")
public boolean isConnected() {
ConnectivityManager cm = (ConnectivityManager) AnkiDroidApp.getInstance().getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null) {
return false;
}
/* NetworkInfo is deprecated in API 29 so we have to check separately for higher API Levels */
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Network network = cm.getActiveNetwork();
if (network == null) {
return false;
}
NetworkCapabilities networkCapabilities = cm.getNetworkCapabilities(network);
if (networkCapabilities == null) {
return false;
}
boolean isInternetSuspended = !networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED);
return networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
&& networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
&& !isInternetSuspended;
} else {
android.net.NetworkInfo networkInfo = cm.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
}

记住抑制警告。

关于java - 将目标 sdk 更新到 29 后导入已弃用的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58975139/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com