用户 10632 和当前进程都没有 android.permission.READ_PHONE_STATE 错误 Android 6

neither user 10632 nor current process has android.permission.READ_PHONE_STATE error in Android 6

我正在 Android 6 中开发一个 Android 应用程序,我在服务中的应用程序需要获取设备 ID (getDeviceId)。我在清单文件中设置了 android.permission.READ_PHONE_STATE,但是在 运行 时我得到了这个错误:

neither user 10632 nor current process has android.permission.READ_PHONE_STATE. java.lang.SecurityException:getDeviceId need android.permission.Read_Phone_state

我使用 adb install myapk.apk 安装我的 apk 文件,我想所有应用程序所需的权限都在安装时授予。

在 android 6+ 中,您需要在运行时请求权限。访问 android 开发者页面,了解如何为 android M 及以上访问请求权限:https://developer.android.com/training/permissions/requesting.html

您还需要在 运行 时请求许可。

int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
    Manifest.permission.READ_PHONE_STATE);

If the app has the permission, the method returns PackageManager.PERMISSION_GRANTED, and the app can proceed with the operation. If the app does not have the permission, the method returns PERMISSION_DENIED, and the app has to explicitly ask the user for permission.

如果你没有得到它,你应该请求它

ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_PHONE_STATE},
                MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);