最近接入了一个新的SDK,初始化接口需求传入当前设备的IP和UA作为参数。本文介绍如何获取设备的IP和UA。

获取IP

运用WIFI联网与不运用WIFI,获取到的IP地址不同。因此,需求先判别当前设备经过哪种方式联网,然后再获取对应的IP地址。

  • 判别网络连接类型

经过ConnectivityManager判别网络连接类型,代码如下:

private fun checkCurrentNetworkType() {
    val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    connectivityManager.run {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            when (activeNetworkInfo?.type) {
                ConnectivityManager.TYPE_MOBILE -> {
                    // 经过手机流量
                }
                ConnectivityManager.TYPE_WIFI -> {
                    // 经过WIFI
                }
                else -> {}
            }
        } else {
            // Android M 以上主张运用getNetworkCapabilities API
            activeNetwork?.let { network ->
                getNetworkCapabilities(network)?.let { networkCapabilities ->
                    if (networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
                        when {
                            networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> {
                                // 经过手机流量
                            }
                            networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> {
                                // 经过WIFI
                            }
                        }
                    }
                }
            }
        }
    }
}
  • 获取手机卡联网 IP

经过NetworkInterface获取IPV4地址,代码如下:

NetworkInterface.getNetworkInterfaces().let {
    loo@ for (networkInterface in Collections.list(it)) {
        for (inetAddresses in Collections.list(networkInterface.inetAddresses)) {
            if (!inetAddresses.isLoopbackAddress && !inetAddresses.isLinkLocalAddress) {
                // IP地址
                val mobileIp = inetAddresses.hostAddress
                break@loo
            }
        }
    }
}
  • 获取WIFI联网 IP

经过ConnectivityManagerWifiManager来获取IP地址,代码如下:

private fun getWIFIIp() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
        val wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
        // IP 地址
        val wifiIp = Formatter.formatIpAddress(wifiManager.connectionInfo.ipAddress)
    } else {
        // Android Q 以上主张运用getNetworkCapabilities API
        val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        connectivityManager.run {
            activeNetwork?.let { network ->
                (getNetworkCapabilities(network)?.transportInfo as? WifiInfo)?.let { wifiInfo ->
                    // IP 地址
                    val wifiIp = Formatter.formatIpAddress(wifiInfo.ipAddress)
                }
            }
        }
    }
}

获取UA

获取设备的UserAgent比较简单,代码如下:

// 体系 UA
System.getProperty("http.agent")
// WebView UA
WebSettings.getDefaultUserAgent(context)

示例

在示例Demo中添加了相关的演示代码。

ExampleDemo github

ExampleDemo gitee

作用如图:

Android 获取IP和UA