Vijai
Vijai

Reputation: 1137

MediaProjection service type not recognized in android Q

With the new privacy changes in effect with Android Q, it is now mandatory for any app using MediaProjection api to specify android:foregroundServiceType attribute in the service tag under manifest.

But recently, I noticed that though I set the android:foregroundServiceType="mediaprojection" there is a security exception thrown. Does anybody have any idea what I'm doing wrong?

Manifest:

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

Exception thrown:

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: This is experienced on Android Q DP5 GSI with target SDK 29. The app works fine with target sdk 28 without any changes.

Upvotes: 12

Views: 14130

Answers (2)

Divyang M
Divyang M

Reputation: 111

Changing the following attributes

android:foregroundServiceType="mediaProjection"

tools:targetApi="q"

in AndroidManifest.xml worked for me.

< service

android:name="SERVICE_NAME"

android:foregroundServiceType="mediaProjection"

tools:targetApi="q"

android:enabled="true"

android:exported="false" />

Upvotes: 4

Oleksii K.
Oleksii K.

Reputation: 5419

Don't you forget to add permission <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> into AndroidManifest.xml ?

UPDATE

Make sure you call startForeground() before getMediaProjection()!

The best solution is to call startForeground() from onCreate() without any conditions, displaying some sort of default messages in notification. Then you can execute your logic and call NotificationManager.notify() with updated notification at any time.

Upvotes: 11

Related Questions