【问题标题】:ConnectivityManager.TYPE_WIFI is showing deprecated in code.I had use Network Capabilities in above M version , want to remove warning of deprecatedConnectivityManager.TYPE_WIFI 在代码中显示已弃用。我在 M 以上版本中使用了网络功能,想要删除已弃用的警告
【发布时间】:2019-10-14 16:09:39
【问题描述】:

我想删除在 m 版本下面的代码中显示的警告。我使用了下面的代码,它运行良好,但仍然想删除在 ConnectivityManager.TYPE_WIFI 行中显示的警告。可以删除下面的警告也显示在 kotlin 编译器中??

val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            cm?.run {
                cm.getNetworkCapabilities(cm.activeNetwork)?.run {
                    if (hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                        return true
                    } else if (hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
                        return true
                    }
                }
            }
        } else {
            cm?.run {
                cm.activeNetworkInfo?.run {
                    if (type == ConnectivityManager.TYPE_WIFI) {
                        return true
                    } else if (type == ConnectivityManager.TYPE_MOBILE) {
                        return true
                    }
                }
            }
        }

警告:- 'getter for type: Int' 已弃用。

【问题讨论】:

  • TL;DR:我不认为你可以。 Lint 目前不检查弃用的代码(仅弃用的 xml 属性和库:android.googlesource.com/platform/tools/base/+log/…),因此 Java/Kotlin 编译器通过查找 @Deprecated 注释来检查弃用。您需要进行检查,知道这是特定 Android 版本 (
  • 是的,我想更改版本 16 的 else 条件代码,以便可以删除警告,并且我可以检查网络状况而不会在 29 版本上发出弃用警告。
  • 我之前检查过这个链接。请在评论之前仔细检查代码是否重复。我想支持所有版本,不仅是 NetworkCapabilities 支持的 M 版本以上。

标签: android kotlin android-connectivitymanager


【解决方案1】:

试试这个

您可以使用 @Suppress("DEPRECATION") 删除该警告

示例代码

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class Main2Activity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)

        val networkResult = getConnectionType(this)

    }

    @Suppress("DEPRECATION")
    fun getConnectionType(context: Context): Boolean {
        var result = false
        val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            cm?.run {
                cm.getNetworkCapabilities(cm.activeNetwork)?.run {
                    if (hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                        result = true
                    } else if (hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
                        result = true
                    }
                }
            }
        } else {
            cm?.run {
                cm.activeNetworkInfo?.run {
                    if (type == ConnectivityManager.TYPE_WIFI) {
                        result = true
                    } else if (type == ConnectivityManager.TYPE_MOBILE) {
                        result = true
                    }
                }
            }
        }
        return result
    }
}

检查屏幕截图

【讨论】:

  • 我不想使用@Suppress("DEPRECATION")。这不是正确的做法。我想更改代码或替换代码以便警告自动消失不使用弃用代码。
  • @AnkitGupta 实际上使用@Suppress("DEPRECATION") 是处理这个问题的正确方法。弃用是一个警告,只要您支持具有此 API 的版本并使用提供签名的版本进行编译,一切都很好。如果您的最小 SDK 设置为 21,您也可以只使用 if-branch,因为它是在 Android L 中引入的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 2012-08-07
  • 2017-05-25
  • 1970-01-01
  • 2017-08-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多