繁体   English   中英

获取用户设备操作系统版本和 android 的 ip 地址的方法是什么?

[英]Is the way to get users device OS version and ip address for android?

我正在创建android 应用程序并希望在后端获得经过身份验证的用户设备操作系统版本ip 地址 经过一些研究,我没有找到有用的信息,有人可以帮助我吗? :)

您可以通过以下方式获得操作系统版本

val deviceOS = android.os.Build.VERSION;

对于ip 地址

fun getLocalIpAddress(): String? {
    try {
        val en: Enumeration<NetworkInterface> = NetworkInterface.getNetworkInterfaces()
        while (en.hasMoreElements()) {
            val networkInterface: NetworkInterface = en.nextElement()
            val enumerationIpAddress: Enumeration<InetAddress> = networkInterface.inetAddresses
            while (enumerationIpAddress.hasMoreElements()) {
                val inetAddress: InetAddress = enumerationIpAddress.nextElement()
                if (!inetAddress.isLoopbackAddress && inetAddress is Inet4Address) {
                    return inetAddress.getHostAddress()
                }
            }
        }
    } catch (ex: SocketException) {
        ex.printStackTrace()
    }
    return null
}

这是 OsVesion 的 java :

  String osVersion = Build.VERSION.RELEASE + " " + Build.VERSION_CODES.class.getFields()[android.os.Build.VERSION.SDK_INT].getName();

对于 IP:如果是 mobileData

    public static String getMobileIPAddress() {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    return addr.getHostAddress();
                }
            }
        }
    } catch (Exception ex) {
    } // for now eat exceptions
    return "";
}

对于 IP:如果是 WiFi

public String getWifiIPAddress() {
    WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
    WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
    int ip = wifiInfo.getIpAddress();
    return Formatter.formatIpAddress(ip);
}

检查它是移动数据还是 WiFi:

            if (getWifiIPAddress().equals("0.0.0.0")) {
                ipAddress = getMobileIPAddress();
            } else {
                ipAddress = getWifiIPAddress();
            }

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM