在将comp和targetSdkVersion
更新为build.gradle
中的“ 29”后,我收到了弃用警告:
warning: [deprecation] NetworkInfo in android.net has been deprecated
import android.net.NetworkInfo;
^
因此,构建失败。但是我使用网络信息来与api版本16-29兼容。我应如何处理针对API“ 29”及更高版本的不推荐使用的导入?
最佳答案
您的问题的答案在此 Link中!
API 29没有弃用NetworkCapabilities,但它需要API 21,因此我仅在API 29上调用过。
public static boolean isNetworkAvailable(Context context) {
if(context == null) return false;
ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
NetworkCapabilities capabilities =
connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
if (capabilities != null) {
if
(capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
return true;
} else if
(capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
return true;
} else if
(capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)){
return true;
}
}
}
else {
try {
NetworkInfo activeNetworkInfo =
connectivityManager.getActiveNetworkInfo();
if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
Log.i("update_statut", "Network is available : true");
return true;
}
} catch (Exception e) {
Log.i("update_statut", "" + e.getMessage());
}
}
}
Log.i("update_statut","Network is available : FALSE ");
return false;
}
关于java - 将目标sdk更新为29后导入不推荐使用的警告,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58975139/