将目标sdk更新为29

时间:2019-11-21 12:31:05

标签: java android deprecated

在更新编译并将targetSdkVersion中的build.gradle设置为“ 29”之后我收到弃用警告:

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

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

2 个答案:

答案 0 :(得分:0)

您的问题的答案就在此 Link中!

NetworkCapabilities在API 29中并未被弃用,但它需要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;
}

答案 1 :(得分:0)

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

@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();
        }
    }

记得取消警告。