android Q 中无法识别 MediaProjection 服务类型

MediaProjection service type not recognized in android Q

随着 Android Q 生效的新隐私更改,现在所有使用 MediaProjection api 的应用都必须在服务标签中指定 android:foregroundServiceType 属性在清单下。

但最近,我注意到尽管我设置了 android:foregroundServiceType="mediaprojection",但仍会引发安全异常。有人知道我做错了什么吗?

清单:

<service
            android:name=".services.MediaProjectionService"
            android:enabled="true"
            android:exported="false"
            android:foregroundServiceType="mediaProjection" />

抛出异常:

java.lang.RuntimeException: Unable to start service com.package.app.services.MediaProjectionService@6d0fed2 with Intent { act=com.package.app.services.action.startrecording cmp=com.package.app/.services.MediaProjectionService(has extras) }: java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4103)
        at android.app.ActivityThread.access00(ActivityThread.java:219)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7343)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:933)
     Caused by: java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
        at android.os.Parcel.createException(Parcel.java:2071)
        at android.os.Parcel.readException(Parcel.java:2039)
        at android.os.Parcel.readException(Parcel.java:1987)
        at android.media.projection.IMediaProjection$Stub$Proxy.start(IMediaProjection.java:231)
        at android.media.projection.MediaProjection.<init>(MediaProjection.java:58)
        at android.media.projection.MediaProjectionManager.getMediaProjection(MediaProjectionManager.java:104)
        at com.package.app.services.MediaProjectionService.startRecording(MediaProjectionService.java:190)
        at com.package.app.services.MediaProjectionService.onStartCommand(MediaProjectionService.java:142)
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4085)
        at android.app.ActivityThread.access00(ActivityThread.java:219) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7343) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:933) 
     Caused by: android.os.RemoteException: Remote stack trace:
        at com.android.server.media.projection.MediaProjectionManagerService$MediaProjection.start(MediaProjectionManagerService.java:476)
        at android.media.projection.IMediaProjection$Stub.onTransact(IMediaProjection.java:135)
        at android.os.Binder.execTransactInternal(Binder.java:1021)
        at android.os.Binder.execTransact(Binder.java:994)

P.S:这是在使用目标 SDK 29 的 Android Q DP5 GSI 上遇到的。该应用程序在目标 sdk 28 上运行良好,无需任何更改。

难道你忘了将权限 <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> 添加到 AndroidManifest.xml 中吗?

更新

确保在 getMediaProjection()!

之前调用 startForeground()

最好的解决方案是不带任何条件地从 onCreate() 调用 startForeground(),在通知中显示某种默认消息。然后你可以执行你的逻辑并随时调用 NotificationManager.notify() 更新通知。

更改以下属性

android:foregroundServiceType="mediaProjection"

tools:targetApi="q"

在 AndroidManifest.xml 为我工作。

< service

android:name="SERVICE_NAME"

android:foregroundServiceType="mediaProjection"

tools:targetApi="q"

android:enabled="true"

android:exported="false" />