在Android Q

时间:2019-07-14 10:21:14

标签: android android-mediaprojection androidq

随着新的隐私权更改在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)

P.S:这是在Android Q DP5 GSI上遇到的。

2 个答案:

答案 0 :(得分:1)

您是否忘了将权限<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />添加到AndroidManifest.xml中?

更新

请务必先致电startForeground(),然后再致电 getMediaProjection()

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

答案 1 :(得分:0)

更改以下属性

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" />