ConnectivityManager.TYPE_WIFI在代码中显示已弃用.我已在M版本以上使用网络功能,想要删除已弃用的警告 [英] ConnectivityManager.TYPE_WIFI is showing deprecated in code.I had use Network Capabilities in above M version , want to remove warning of deprecated
本文介绍了ConnectivityManager.TYPE_WIFI在代码中显示已弃用.我已在M版本以上使用网络功能,想要删除已弃用的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想删除显示在m版本以下代码中的警告.我曾使用下面的代码运行良好,但仍想删除显示在ConnectivityManager.TYPE_WIFI行中的警告.可以删除以下警告吗?还在kotlin编译器中显示??
I want to remove the warning which is showing in the code below m version.I had use below code which is working good but still want to remove the warning which is showing in line ConnectivityManager.TYPE_WIFI.Can below warning be removed which is also showing in kotlin compiler ??
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
}
}
}
}
警告:-不建议使用类型的获取器:Int".
Warning :- 'getter for type: Int' is deprecated.
推荐答案
尝试一下
您可以使用 @Suppress("DEPRECATION")
删除该警告
You can use @Suppress("DEPRECATION")
to remove that warning
示例代码
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
}
}
检查屏幕
这篇关于ConnectivityManager.TYPE_WIFI在代码中显示已弃用.我已在M版本以上使用网络功能,想要删除已弃用的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文