升级targetSDK到34后在Android 14上后台消息通知服务直接崩溃

最近我的可可便签开发已经到收尾阶段,当我把targetSDK升级到34,在Android 14上启动后台服务的直接崩了

Service配置类似如下

<service
    android:name=".service.CocoNotificationService"
    android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
    android:foregroundServiceType="specialUse|dataSync"
    android:exported="false">
    <intent-filter>
        <action android:name="android.service.notification.NotificationListenerService" />
    </intent-filter>
</service>

崩溃日志

FATAL EXCEPTION: main
Process: com.cocozq.note, PID: 29314
java.lang.RuntimeException: Unable to create service com.cocozq.coco_flutter.service.CocoNotificationService: java.lang.SecurityException: Starting FGS with type connectedDevice callerApp=ProcessRecord{d54bbf8 29314:com.cocozq.note/u0a438} targetSDK=34 requires permissions: all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE] any of the permissions allOf=false [android.permission.BLUETOOTH_ADVERTISE, android.permission.BLUETOOTH_CONNECT, android.permission.BLUETOOTH_SCAN, android.permission.CHANGE_NETWORK_STATE, android.permission.CHANGE_WIFI_STATE, android.permission.CHANGE_WIFI_MULTICAST_STATE, android.permission.NFC, android.permission.TRANSMIT_IR, android.permission.UWB_RANGING, USB Device, USB Accessory]
at android.app.ActivityThread.handleCreateService(ActivityThread.java:4953)
at android.app.ActivityThread.-$$Nest$mhandleCreateService(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2456)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:257)
at android.os.Looper.loop(Looper.java:368)
at android.app.ActivityThread.main(ActivityThread.java:8848)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:572)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049)
Caused by: java.lang.SecurityException: Starting FGS with type connectedDevice callerApp=ProcessRecord{d54bbf8 29314:com.cocozq.note/u0a438} targetSDK=34 requires permissions: all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE] any of the permissions allOf=false [android.permission.BLUETOOTH_ADVERTISE, android.permission.BLUETOOTH_CONNECT, android.permission.BLUETOOTH_SCAN, android.permission.CHANGE_NETWORK_STATE, android.permission.CHANGE_WIFI_STATE, android.permission.CHANGE_WIFI_MULTICAST_STATE, android.permission.NFC, android.permission.TRANSMIT_IR, android.permission.UWB_RANGING, USB Device, USB Accessory]
at android.os.Parcel.createExceptionOrNull(Parcel.java:3087)
at android.os.Parcel.createException(Parcel.java:3071)
at android.os.Parcel.readException(Parcel.java:3054)
at android.os.Parcel.readException(Parcel.java:2996)
at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:6761)
at android.app.Service.startForeground(Service.java:775)
at com.cocozq.coco_flutter.service.CocoNotificationService.initNotify(CocoNotificationService.java:101)
at com.cocozq.coco_flutter.service.CocoNotificationService.onCreate(CocoNotificationService.java:50)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:4928)
at android.app.ActivityThread.-$$Nest$mhandleCreateService(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2456)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:257)
at android.os.Looper.loop(Looper.java:368)
at android.app.ActivityThread.main(ActivityThread.java:8848)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:572)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.am.ActiveServices.validateForegroundServiceType(ActiveServices.java:2698)
at com.android.server.am.ActiveServices.setServiceForegroundInnerLocked(ActiveServices.java:2402)
at com.android.server.am.ActiveServices.setServiceForegroundLocked(ActiveServices.java:1754)
at com.android.server.am.ActivityManagerService.setServiceForeground(ActivityManagerService.java:14277)
at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:3385)
2024-06-30 23:03:27.791 29314-29314 com.cocozq.note com.cocozq.note W Attempt to remove non-JNI local reference
2024-06-30 23:03:27.817 29314-29314 Process com.cocozq.note I Sending signal. PID: 29314 SIG: 9

原因

从日志看出Android 14开始对Forgroud服务进行了权限检测,但是加上权限并且确认获得权限的情况下依然崩溃,最后在google官方问题追踪系统看到这是系统bug。

解决办法

  1. targetSDK回退33,推荐使用
  2. 在对应权限上加上remove属性
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" tools:remove=""/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" tools:remove=""/>

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

 桂ICP备15001694号-3