Crash: ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION

298 views Asked by At

I've got a video streaming app that has been running well for a number of years but I recently upgraded to

targetSdkVersion 28

I'm now seeing the following error...

java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
        at android.os.Parcel.createExceptionOrNull(Parcel.java:2373)
        at android.os.Parcel.createException(Parcel.java:2357)
        at android.os.Parcel.readException(Parcel.java:2340)
        at android.os.Parcel.readException(Parcel.java:2282)
        at android.media.projection.IMediaProjection$Stub$Proxy.start(IMediaProjection.java:231)
        at android.media.projection.MediaProjection.<init>(MediaProjection.java:59)
        at android.media.projection.MediaProjectionManager.getMediaProjection(MediaProjectionManager.java:119)
        at screencast.StreamerService.startScreenCast(StreamerService.java:401)
        at screencast.ScreencastBusinessLogic.loginToFacebook(ScreencastBusinessLogic.java:191)
        at screencast.ScreencastBusinessLogic.access$200(ScreencastBusinessLogic.java:52)
        at screencast.ScreencastBusinessLogic$4.onCompleted(ScreencastBusinessLogic.java:449)
        at com.facebook.GraphRequest$5.run(GraphRequest.java:1398)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7660)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: android.os.RemoteException: Remote stack trace:
        at com.android.server.media.projection.MediaProjectionManagerService$MediaProjection.start(MediaProjectionManagerService.java:479)
        at android.media.projection.IMediaProjection$Stub.onTransact(IMediaProjection.java:135)
        at android.os.Binder.execTransactInternal(Binder.java:1154)
        at android.os.Binder.execTransact(Binder.java:1123)

I followed the suggestions at Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE TYPE_MEDIA_PROJECTION in Android Pie and Q without any luck.

My manifest includes

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <application
...
        <service
            android:name="screencast.StreamerService"
            android:foregroundServiceType="mediaProjection"
            android:enabled="true"
            android:exported="true"
            tools:targetApi="q"
            />
    </application>

The service is called like so...

package screencast;
final public class StreamerService extends Service implements Streamer.Listener {
...

I'm able to successfully request the permission (get a result code of -1 in onActivityResult like so...

   public void requestMediaProjection() {
        //Log.v(TAG, "requestMediaProjection");
        final MediaProjectionManager manager = (MediaProjectionManager) context.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        context.startActivityForResult(
                manager.createScreenCaptureIntent(),
                REQUEST_MEDIA_PROJECTION);
    }


int resultCode;
Intent data;
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    this.resultCode = resultCode;
    this.data = data;
    if (requestCode != REQUEST_MEDIA_PROJECTION) {
        return;
    }
    if (resultCode != Activity.RESULT_OK) {
        return;
    }
    enterStreamDescription();
}

I get the crash later on here when I call manager.getMediaProjection...

   public void startScreenCast(int resultCode,
                         Intent data,
                         int screenDensity,
                         List<Connection> connections,
                         AudioConfig audioConfig,
                         VideoConfig videoConfig,
                         Streamer.MODE mode,
                         File mp4,
                                MediaProjectionManager manager
    ) {

   ...

        mMediaProjection = manager.getMediaProjection(resultCode, data);

    }

Any suggestions are greatly appreciated. Thanks.

0

There are 0 answers