简体   繁体   English

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

[英]Alternative to NetworkInfo.isConnected() (Deprecated in API 23) for Network objects

I want to use ConnectivityManager to check if an active.network can access the inte.net or not ie.我想使用ConnectivityManager来检查 active.network 是否可以访问 inte.net,即。 getActiveNetworkInfo().isConnected() . getActiveNetworkInfo().isConnected()

I read that we can still use getActiveNetwork() to get an active.network, but there doesn't seem to be a similar method like isConnected for Network objects.我读到我们仍然可以使用getActiveNetwork()来获取 active.network,但是对于Network对象似乎没有类似isConnected的方法。 How to workaround?如何解决?

The deprecated code is found here: https://codelabs.developers.google.com/codelabs/android-training-asynctask-asynctaskloader/index.html?index=..%2F..%2Fandroid-training#4已弃用的代码可在此处找到: https://codelabs.developers.google.com/codelabs/android-training-asynctask-asynctaskloader/index.html?index=..%2F..%2Fandroid-training#4

You can use this method to check the connectivity.您可以使用此方法检查连通性。 connectivityManager.getActiveNetwork() is added in SDK 29.在 SDK 29 中添加了连接管理器.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;
}

In kotlin:在 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
    }

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

  • getActiveNetworkInfo deprecated on API 29. API 29 上弃用了getActiveNetworkInfo
  • getAllNetworkInfo deprecated on API 23. getAllNetworkInfo 23 上弃用了 getAllNetworkInfo。

You should use this code.您应该使用此代码。


   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
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NetworkInfo.isConnected()返回true但没有连接 - NetworkInfo.isConnected() returns true but no connection NetworkInfo.isConnected()和NetworkInfo.getDetailedState()== NetworkInfo.DetailedState.CONNECTED之间的区别。 用什么? - Difference between NetworkInfo.isConnected() and NetworkInfo.getDetailedState() == NetworkInfo.DetailedState.CONNECTED. What to use? 网络信息和 WifiConfiguration 在 SDK 22 中已被弃用 - NetworkInfo and WifiConfiguration has been deprecated in SDK 22 由于 Android getFragmentManager() API 已弃用,是否还有其他选择? - Since the Android getFragmentManager() API is deprecated, is there any alternative? 'com.google.appengine.api.backends.BackendService'已弃用 - 'com.google.appengine.api.backends.BackendService' is deprecated alternative 在API级别23中删除了org.apache.http包。有什么替代方案? - org.apache.http packages removed in API level 23. What is the alternative? NetworkInfo无法正确响应运行Nougat的手机上的网络更改 - NetworkInfo is not properly responding to network changes on phone running Nougat 如果不推荐使用Swing,那么替代方案是什么? - If Swing is deprecated, what is the alternative? 不推荐使用的替代方法getCellType - Alternative to deprecated getCellType 已弃用的“GoogleCredential”的替代方法是什么? - What is the alternative to the deprecated 'GoogleCredential'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM