昨天我设置了 upped 目标并将 sdk 编译到 23 API 级别。当我在 Nexus 9 上使用 Preview Android M (MPA441) 启动应用程序时,应用程序崩溃并出现以下SecurityException:
FATAL EXCEPTION: main
Process: com.grouplogic.mobilecho, PID: 22554
java.lang.RuntimeException: Unable to create application com.mypackage.MyApplication: java.lang.SecurityException: getDeviceId: Neither user 10098 nor current process has android.permission.READ_PHONE_STATE.
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4710)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.SecurityException: getDeviceId: Neither user 10098 nor current process has android.permission.READ_PHONE_STATE.
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)
at com.android.internal.telephony.ITelephony$Stub$Proxy.getDeviceId(ITelephony.java:4175)
at android.telephony.TelephonyManager.getDeviceId(TelephonyManager.java:706)
at com.mypackage.utils.DeviceUtils.isRunOnEmulator(DeviceUtils.java:63)
at com.mypackage.utils.DeviceUtils.getDeviceID(DeviceUtils.java:49)
at com.mypackage.domain.service.SettingService.setupPreferences(SettingService.java:79)
at com.mypackage.app.BaseApplication.createSingletons(BaseApplication.java:84)
at com.mypackage.app.BaseApplication.onCreate(BaseApplication.java:66)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1013)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4707)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
当我通过 Android Studio 安装应用程序时,有人知道为什么新安装的应用程序默认禁用该权限吗?避免这种崩溃的正确方法是什么,也许有办法检查启用或不启用目标权限,并且只有启用它,应用程序才应该使用某些特定的设备 API(读取手机状态、联系人和其他权限)?在互联网上,我刚刚发现与此问题1相关的提及,但我没有找到任何明确的解释:是 Android M 中的错误还是开发人员无法理解如何以安全的方式使用权限!?我也读过这篇文章但是当用户应该去设置并启用应用程序权限时的方法看起来很奇怪而且对用户不友好。我想如果用户从 Google Play 安装应用程序,一切都会好起来的,因为他明确申请了请求的权限,但我需要确认。