简体   繁体   English

如何在 Android 10 中获取有关手机 IMEI 的信息?

[英]How do I get information about a phone's IMEI in Android 10?

Before Android 10, I was using the TelephonyManager API to retrieve that info, but it's no longer working in Android 10.在 Android 10 之前,我使用TelephonyManager API 来检索该信息,但它不再在 Android 10 中工作。

From the Android Developers Documentation Website来自Android 开发者文档网站

Starting in Android 10, apps must have the READ_PRIVILEGED_PHONE_STATE privileged permission in order to access the device's non-resettable identifiers, which include both IMEI and serial number.从 Android 10 开始,应用程序必须具有READ_PRIVILEGED_PHONE_STATE特权权限才能访问设备的不可重置标识符,其中包括 IMEI 和序列号。

Third-party apps installed from the Google Play Store cannot declare privileged permissions.从 Google Play 商店安装的第三方应用程序无法声明特权权限。

If your app doesn't have the permission and you try asking for information about non-resettable identifiers anyway, the platform's response varies based on target SDK version:如果您的应用没有权限并且您尝试询问有关不可重置标识符的信息,平台的响应会因目标 SDK 版本而异:

  • If your app targets Android 10 or higher, a SecurityException occurs.如果您的应用程序以 Android 10 或更高版本为目标,则会发生 SecurityException。
  • If your app targets Android 9 (API level 28) or lower, the method returns null or placeholder data if the app has the READ_PHONE_STATE permission.如果您的应用程序以 Android 9(API 级别 28)或更低版本为目标,则如果应用程序具有READ_PHONE_STATE权限,则该方法返回 null 或占位符数据。 Otherwise, a SecurityException occurs.否则,会发生 SecurityException。

If you try to access it, it throws the below exception: java.lang.SecurityException: getImeiForSlot: The user 10180 does not meet the requirements to access device identifiers.如果尝试访问它会抛出以下异常: java.lang.SecurityException: getImeiForSlot: The user 10180 does not meet the requirements to access device identifiers.

I'm facing the same problem, but, in my case, I have a kind of "backup" code that returns a UUID.我面临同样的问题,但就我而言,我有一种返回 UUID 的“备份”代码。 Here is a code that you could use:这是您可以使用的代码:

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

This code is usefull if you want a "instalation unique identifier" but, doesn't works as a device unique identifier because if the user unistall and re install your app, the UUID returned will be different than the last one.如果您想要“安装唯一标识符”,此代码很有用,但它不能用作设备唯一标识符,因为如果用户卸载并重新安装您的应用程序,返回的 UUID 将与上一个不同。

In my case, I use the UUID.nameUUIDFromBytes to generated a UUID by a given "name" and I'm using the Settings.Secure.ANDROID_ID as the "name" for the UUID.就我而言,我使用 UUID.nameUUIDFromBytes 通过给定的“名称”生成 UUID,并且我使用 Settings.Secure.ANDROID_ID 作为 UUID 的“名称”。 Using this method you "grantee" the returned UUID will be the same, UNLESS the user do a factory reset.使用此方法,您“授予”返回的 UUID 将是相同的,除非用户进行出厂重置。

Here is the code:这是代码:

   String androidId = Settings.Secure.getString(context.getContentResolver(),
            Settings.Secure.ANDROID_ID);

   UUID androidId_UUID = UUID
            .nameUUIDFromBytes(androidId.getBytes("utf8"));

   String unique_id = androidId_UUID.toString();

Until here, everything seens ok, but the problem is: since Android 10 was released, Google doesn't recommend the uses of any kind of "hardware indentifier" and this includes the Settings.Secure.ANDROID_ID.到这里为止,一切正常,但问题是:自从 Android 10 发布以来,Google 不建议使用任何类型的“硬件标识符”,这包括 Settings.Secure.ANDROID_ID。 This actually is my concern, because in the company that I work for, we use the IMEI or this UUID to identify our customers users and define if an user is trying to log in more than one device, which is not allowed by our rules, and to build some statics.这实际上是我关心的问题,因为在我工作的公司中,我们使用 IMEI 或此 UUID 来识别我们的客户用户并定义用户是否尝试登录多个设备,这是我们的规则所不允许的,并建立一些静力学。 If the UUID isn't unique for the same device we'll have to review all of our user access control.如果同一设备的 UUID 不是唯一的,我们将不得不审查我们所有的用户访问控制。

Here is the Android Developers link about unique identifiers good pratices.这是关于唯一标识符良好实践的 Android 开发人员链接。 https://developer.android.com/training/articles/user-data-ids https://developer.android.com/training/articles/user-data-ids

And here is the same link, but with an anchor where Google describes some use cases and the best option of unique identifier for each one.这是相同的链接,但有一个锚点,谷歌在其中描述了一些用例以及每个用例的唯一标识符的最佳选择。 https://developer.android.com/training/articles/user-data-ids#common-use-cases https://developer.android.com/training/articles/user-data-ids#common-use-cases

None of use cases fit with mine, so I'm still loking for a better solution.没有一个用例适合我,所以我仍在寻找更好的解决方案。

I hope this could help someone.我希望这可以帮助某人。

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

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