gpt4 book ai didi

Android:不推荐使用 WifiInfo.getIpAddress()

转载 作者:行者123 更新时间:2023-12-05 00:43:09 26 4
gpt4 key购买 nike

API 31 已弃用 getIpAddress() 需要帮助从回调实现中获取地址

val networkCallback = object : ConnectivityManager.NetworkCallback() {

override fun onAvailable(network: Network) {}


@RequiresApi(Build.VERSION_CODES.Q)
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
super.onCapabilitiesChanged(network, networkCapabilities)
val wifiInfo = networkCapabilities.transportInfo as? WifiInfo
}

override fun onLinkPropertiesChanged(network: Network, linkProperties: LinkProperties) {
super.onLinkPropertiesChanged(network, linkProperties)
// val ipV4Address = ?

}
};

最佳答案

对于不推荐使用的方法,您可以阅读官方 android 文档,作为documnet syas:

This method was deprecated in API level 31. Use the methods on LinkProperties which can be obtained either via NetworkCallback#onLinkPropertiesChanged(Network, LinkProperties) or ConnectivityManager#getLinkProperties(Network).

所以通过我在这里添加的这种方法,你可以获得所有的IP地址:

   private fun getIpAddress() {
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE)
if (connectivityManager is ConnectivityManager) {
var link:LinkProperties = connectivityManager.getLinkProperties(connectivityManager.activeNetwork) as LinkProperties
Log.e("NETWOOOOOOOOOOOORK", link.linkAddresses.toString())
}

}

注意: link.linkAddresses 方法可能返回地址数组

关于Android:不推荐使用 WifiInfo.getIpAddress(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71003749/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com