java 用于网络对象的NetworkInfo.isConnected()(在API 23中已弃用)的替代方法

z8dt9xmd  于 2023-02-02  发布在  Java
关注(0)|答案(3)|浏览(193)

我想使用ConnectivityManager来检查一个活动的网络是否可以访问互联网。
我读到我们仍然可以使用getActiveNetwork()来获得一个活跃的网络,但是对于Network对象似乎没有类似isConnected的方法。
弃用代码可在以下位置找到:https://codelabs.developers.google.com/codelabs/android-training-asynctask-asynctaskloader/index.html?index=..%2F..%2Fandroid-training#4

jjhzyzn0

jjhzyzn01#

您可以使用此方法检查连接性。SDK 29中添加了connectivityManager.getActiveNetwork()

public static boolean isNetworkAvailable(Context context) {
    if (context == null) return false;

    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connectivityManager != null) {
        if (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;
                }
                if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                    return true;
                }
                return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET);
            } else {
                return false;
            }
        } else {
            NetworkInfo info = connectivityManager.getActiveNetworkInfo();
            return info != null && info.isConnected();
        }
    }
    return false;
}
piok6c0g

piok6c0g2#

NetworkInfo已弃用:https://developer.android.com/reference/android/net/NetworkInfo

  • getActiveNetworkInfo在API 29上已弃用。
  • getAllNetworkInfo在API 23上已弃用。

您应该使用此代码。

private fun isAvailableNetwork(context: Context): Boolean {
        val connectivityManager =
            context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val network = connectivityManager.activeNetwork ?: return false
        val networkCapabilities =
            connectivityManager.getNetworkCapabilities(network) ?: return false
        return when {
            networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
            networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
            networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
            networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH) -> true
            else -> false
        }
    }
44u64gxh

44u64gxh3#

您应该使用SDK_INT〉= Q的新实现,对于旧版本,您应该使用旧API并抑制弃用,如下所示:
在Kotlin:

val Context.isNetworkConnected: Boolean
    get() {
        val manager = getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
        return if (SDK_INT >= Q)
            manager.getNetworkCapabilities(manager.activeNetwork)?.let {
                it.hasTransport(TRANSPORT_WIFI) || it.hasTransport(TRANSPORT_CELLULAR) ||
                        it.hasTransport(TRANSPORT_BLUETOOTH) ||
                        it.hasTransport(TRANSPORT_ETHERNET) ||
                        it.hasTransport(TRANSPORT_VPN)
            } ?: false
        else
            @Suppress("DEPRECATION")
            manager.activeNetworkInfo?.isConnected == true
    }

相关问题