如何在应用程序中获取我的Android手机的UUID?

我正在寻求帮助,以获得我的Android手机的UUID。 我search了networking,发现了一个潜在的解决scheme,但它不在模拟器中工作。

这里是代码:

Class<?> c; try { c = Class.forName("android.os.SystemProperties"); Method get = c.getMethod("get", String.class); serial = (String) get.invoke(c, "ro.serialno"); Log.d("ANDROID UUID",serial); } catch (Exception e) { e.printStackTrace(); } 

有人知道为什么它不工作,或有更好的解决scheme吗?

这适用于我:

 TelephonyManager tManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); String uuid = tManager.getDeviceId(); 

编辑:

你还需要在你的Manifest中设置android.permission.READ_PHONE_STATE 。 从Android M开始,您需要在运行时问这个权限。

看到这个安卓: https ://stackoverflow.com/a/38782876/1339179

正如戴夫·韦伯(Dave Webb)提到的那样, Android开发人员博客有一篇文章涵盖了这一点。 他们首选的解决scheme是跟踪应用程序安装而不是设备,这对于大多数使用情况来说都是很好的。 博客文章将向您展示必要的代码来完成这项工作,我build议您检查一下。

但是,如果您需要设备标识符而不是应用程序安装标识符,则该博客继续讨论解决scheme。 我和Google的某个人谈过话,以便在需要的时候对几件事情做进一步的说明。 以下是我在上述博客文章中未提及的关于设备标识符的发现:

  • ANDROID_ID是首选的设备标识符。 ANDROID_ID在Android <= 2.1或> = 2.3的版本上是完全可靠的。 只有2.2有这个post中提到的问题。
  • 2.2中有几个厂商的几个设备受到ANDROID_ID的影响。
  • 据我所知,所有受影响的设备都有相同的ANDROID_ID ,即9774d56d682e549c 。 这也是仿真器报告的相同的设备ID,顺便说一句。
  • 谷歌认为,原始设备制造商已经为他们的许多或大部分设备修补了这个问题,但是我能够validation到2011年4月初,至lessfind具有破坏的ANDROID_ID的设备还是很容易的。
  • 当一个设备有多个用户(在运行Android 4.2或更高版本的某些设备上可用)时 ,每个用户都显示为完全独立的设备,因此ANDROID_ID值对每个用户都是唯一的。

根据Google的build议,我实现了一个类,它将为每个设备生成一个唯一的UUID,并在适当的时候使用ANDROID_ID作为种子,必要时返回TelephonyManager.getDeviceId(),如果失败,则使用随机生成的唯一UUID这是跨应用程序重新启动(但不是应用程序重新安装)持久。

请注意,对于必须在设备ID上回退的设备,唯一ID 在工厂重置期间持续存在。 这是要注意的事情。 如果您需要确保出厂重置将重置您的唯一ID,则可能需要考虑直接返回到随机UUID而不是设备ID。

再次,这个代码是为了一个设备ID,而不是一个应用程序的安装ID。 对于大多数情况下,应用程序安装ID可能是你正在寻找的。 但是,如果您确实需要设备ID,那么下面的代码可能适合您。

 import android.content.Context; import android.content.SharedPreferences; import android.provider.Settings.Secure; import android.telephony.TelephonyManager; import java.io.UnsupportedEncodingException; import java.util.UUID; public class DeviceUuidFactory { protected static final String PREFS_FILE = "device_id.xml"; protected static final String PREFS_DEVICE_ID = "device_id"; protected static UUID uuid; public DeviceUuidFactory(Context context) { if( uuid ==null ) { synchronized (DeviceUuidFactory.class) { if( uuid == null) { final SharedPreferences prefs = context.getSharedPreferences( PREFS_FILE, 0); final String id = prefs.getString(PREFS_DEVICE_ID, null ); if (id != null) { // Use the ids previously computed and stored in the prefs file uuid = UUID.fromString(id); } else { final String androidId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID); // Use the Android ID unless it's broken, in which case fallback on deviceId, // unless it's not available, then fallback on a random number which we store // to a prefs file try { if (!"9774d56d682e549c".equals(androidId)) { uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8")); } else { final String deviceId = ((TelephonyManager) context.getSystemService( Context.TELEPHONY_SERVICE )).getDeviceId(); uuid = deviceId!=null ? UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")) : UUID.randomUUID(); } } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } // Write the value out to the prefs file prefs.edit().putString(PREFS_DEVICE_ID, uuid.toString() ).commit(); } } } } } /** * Returns a unique UUID for the current android device. As with all UUIDs, this unique ID is "very highly likely" * to be unique across all Android devices. Much more so than ANDROID_ID is. * * The UUID is generated by using ANDROID_ID as the base key if appropriate, falling back on * TelephonyManager.getDeviceID() if ANDROID_ID is known to be incorrect, and finally falling back * on a random UUID that's persisted to SharedPreferences if getDeviceID() does not return a * usable value. * * In some rare circumstances, this ID may change. In particular, if the device is factory reset a new device ID * may be generated. In addition, if a user upgrades their phone from certain buggy implementations of Android 2.2 * to a newer, non-buggy version of Android, the device ID may change. Or, if a user uninstalls your app on * a device that has neither a proper Android ID nor a Device ID, this ID may change on reinstallation. * * Note that if the code falls back on using TelephonyManager.getDeviceId(), the resulting ID will NOT * change after a factory reset. Something to be aware of. * * Works around a bug in Android 2.2 for many devices when using ANDROID_ID directly. * * @see http://code.google.com/p/android/issues/detail?id=10603 * * @return a UUID that may be used to uniquely identify your device for most purposes. */ public UUID getDeviceUuid() { return uuid; } } 
 <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> 

而不是从TelephonyManager获取IMEI使用ANDROID_ID。

 Settings.Secure.ANDROID_ID 

这适用于每个Android设备,不pipe有电话。

  String id = UUID.randomUUID().toString(); 

查看使用UUID类获取uuid的Android开发者博客文章

  <uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

方法

 String getUUID(){ TelephonyManager teleManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); String tmSerial = teleManager.getSimSerialNumber(); String tmDeviceId = teleManager.getDeviceId(); String androidId = android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID); if (tmSerial == null) tmSerial = "1"; if (tmDeviceId== null) tmDeviceId = "1"; if (androidId == null) androidId = "1"; UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDeviceId.hashCode() << 32) | tmSerial.hashCode()); String uniqueId = deviceUuid.toString(); return uniqueId; } 

从API 26开始,getDeviceId()已被弃用。 如果您需要获取设备的IMEI,请使用以下命令:

  String deviceId = ""; if (Build.VERSION.SDK_INT >= 26) { deviceId = getSystemService(TelephonyManager.class).getImei(); }else{ deviceId = getSystemService(TelephonyManager.class).getDeviceId(); }