我将目标SDK版本升级到31后,telephonyManager.listen无法工作。请考虑以下代码行:
telephonyManager.listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);
logcat错误消息如下:
2022-02-27 13:51:36.313 6426-6426/com.example.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.app, PID: 6426
java.lang.RuntimeException: Unable to start service com.example.app.myPlayService@7ace64e with Intent { cmp=com.example.app/.myPlayService (has extras) }: java.lang.SecurityException: listen
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4657)
at android.app.ActivityThread.access$2000(ActivityThread.java:247)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2091)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: java.lang.SecurityException: listen
at android.os.Parcel.createExceptionOrNull(Parcel.java:2425)
at android.os.Parcel.createException(Parcel.java:2409)
at android.os.Parcel.readException(Parcel.java:2392)
at android.os.Parcel.readException(Parcel.java:2334)
at com.android.internal.telephony.ITelephonyRegistry$Stub$Proxy.listenWithEventList(ITelephonyRegistry.java:1036)
at android.telephony.TelephonyRegistryManager.listenFromListener(TelephonyRegistryManager.java:250)
at android.telephony.TelephonyManager.listen(TelephonyManager.java:5999)
at com.example.app.myPlayService.onStartCommand(myPlayService.java:186)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4639)
at android.app.ActivityThread.access$2000(ActivityThread.java:247)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2091)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.internal.telephony.TelephonyPermissions.enforceCarrierPrivilege(TelephonyPermissions.java:634)
at com.android.internal.telephony.TelephonyPermissions.checkReadPhoneState(TelephonyPermissions.java:149)
at com.android.internal.telephony.TelephonyPermissions.checkCallingOrSelfReadPhoneState(TelephonyPermissions.java:94)
at com.android.server.TelephonyRegistry.checkListenerPermission(TelephonyRegistry.java:3093)
at com.android.server.TelephonyRegistry.listen(TelephonyRegistry.java:1024)
我在 Android Manifest 文件中添加了以下权限,但仍然不起作用。
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
在 Android 12 或以下版本中,它可以完美地运行。如何解决在 Android SDK 31 上的问题?提前感谢。