在安卓 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.access$1800(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.access$1800(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)

PS:这在目标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" />

相关内容

  • 没有找到相关文章

最新更新