这是为android获得用户设备操作系统版本和ip地址的方法吗



我正在创建android应用程序,并希望在后端获取经过身份验证的用户设备OS版本ip地址?经过一些研究,我没有发现有用的信息,有人能帮我吗?:(

这里是java对于OsVesion:

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();
}

您可以使用获得os版本

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
}

最新更新