简体   繁体   English

无法在 Android 10 上以设备所有者身份获取 IMEI

[英]Can't get IMEI as device owner on Android 10

For a special kiosk setup I need the IMEI of the device.对于特殊的信息亭设置,我需要设备的 IMEI。 Since Android 10 it is not possible for regular apps to access device identifiers like the IMEI.从 Android 10 开始,常规应用无法访问 IMEI 等设备标识符。 However the documentation suggests:但是文档建议:

This API requires one of the following:此 API 需要以下条件之一:

  • If the caller is the device or profile owner, the caller holds the Manifest.permission#READ_PHONE_STATE permission.如果呼叫者是设备或配置文件所有者,则呼叫者拥有 Manifest.permission#READ_PHONE_STATE 权限。
  • ... ...

I've set the app as device owner with the following command:我已使用以下命令将该应用程序设置为设备所有者:

adb shell dpm set-device-owner com.xxxx.xxxx/.AdminReceiver

My manifest contains:我的清单包含:

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

The IMEI is accessed with the following lines of code:使用以下代码行访问 IMEI:

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

try
{
    return telephonyManager.getImei();
}
catch (SecurityException e)
{
    return null;
}

I'm still getting SecurityExceptions (with message: The user does not meet the requirements to access device identifiers. ).我仍然收到 SecurityExceptions(带有消息: The user does not meet the requirements to access device identifiers. )。 What am I doing wrong?我究竟做错了什么?

READ_PHONE_STATE is a dangerous permission. READ_PHONE_STATE是一个dangerous权限。 For Android 6.0+, you need to request it at runtime in addition to having it the manifest.对于 Android 6.0+,除了拥有清单之外,您还需要 在运行时请求它 Alternatively — particularly for a one-off app installation — you could go into the system Settings app and manually grant the permission.或者 - 特别是对于一次性应用程序安装 - 您可以进入系统设置应用程序并手动授予权限。

As far as I know, Android 10 restricts IMEI access to system apps only (but you can try with device owner rights).据我所知,Android 10 仅将 IMEI 访问权限限制为系统应用(但您可以尝试使用设备所有者权限)。 To get the IMEI, the app needs to declare the following permission:要获取IMEI,应用程序需要声明以下权限:

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

See for details:https://developer.android.com/about/versions/10/privacy/changes详情请参阅:https ://developer.android.com/about/versions/10/privacy/changes

A workaround would be to target your app to Android 9 (API level 28) or lower .一种解决方法是将您的应用定位到 Android 9(API 级别 28)或更低版本

You can get the example of the device owner app here (this is my open source MDM project).您可以在此处获取设备所有者应用程序的示例(这是我的开源 MDM 项目)。 In that example, the app retrieves IMEI on Android 10 using this way and declaring android.permission.READ_PHONE_state only.在该示例中,该应用使用这种方式在 Android 10 上检索 IMEI,并仅声明android.permission.READ_PHONE_state

UPDATE - July 2020: On some builds of Android 10 (for example Samsung Galaxy A01), the IMEI is now not returned even if the app targets to API level 28.更新 - 2020 年 7 月:在某些 Android 10 版本(例如三星 Galaxy A01)上,即使应用程序面向 API 级别 28,现在也不会返回 IMEI。

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

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