没什么可说的,直接上代码:
1 public class PhoneStateCodeUtils { 2 /** 3 * 获取手机imei串号 4 */ 5 public static String getImei(Context context) { 6 TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 7 //设备串号 8 String imei = tm.getDeviceId(); 9 return imei; 10 } 11 12 /** 13 * 获取手机ip 14 * 15 * @return 16 */ 17 public static String getPhoneIp() { 18 try { 19 for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) { 20 NetworkInterface intf = en.nextElement(); 21 for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) { 22 InetAddress inetAddress = enumIpAddr.nextElement(); 23 if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) { 24 // if (!inetAddress.isLoopbackAddress() && inetAddress 25 // instanceof Inet6Address) { 26 return inetAddress.getHostAddress().toString(); 27 } 28 } 29 } 30 } catch (Exception e) { 31 } 32 return "127.0.0.1"; 33 } 34 35 }
时间: 2024-10-15 02:48:15