Error MediaProjection when targetSdkVersion 29

1.3k Views Asked by At

I am building a screen recording application. But it only works on targetSdkVersion 28, when passing targetSdkVersion 29 requested by google, the error

Code

 @SuppressLint("MissingSuperCall")
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode != REQUEST_CODE) {
            Toast.makeText(this, "Error!", Toast.LENGTH_SHORT).show();
            return;
        }
        if (resultCode != RESULT_OK) {
            Toast.makeText(this,
                    "Screen Cast Permission Denied", Toast.LENGTH_SHORT).show();
            return;
        }
        mMediaProjectionCallback = new MediaProjectionCallback();

        mMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);
        mMediaProjection.registerCallback(mMediaProjectionCallback, null);
        mVirtualDisplay = createVirtualDisplay();
        mMediaRecorder.start();
    }

Error

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.megamestudio.screen_record_and_booster, PID: 21862
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1000, result=-1, data=Intent { (has extras) }} to activity {com.megamestudio.screen_record_and_booster/com.megamestudio.screen_record_and_booster.MainActivity}: java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4927)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4968)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2043)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:224)
        at android.app.ActivityThread.main(ActivityThread.java:7548)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
     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:2074)
        at android.os.Parcel.readException(Parcel.java:2042)
        at android.os.Parcel.readException(Parcel.java:1990)
        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.megamestudio.screen_record_and_booster.MainActivity.onActivityResult(MainActivity.java:426)

Thank so much!!!!

1

There are 1 best solutions below

1
On

There is a new service attribute added in API 29, and apparently MediaProjection requires a foreground service to be used since the exception clarifies that. Here is what you need to do:

  1. Create a service,
  2. Add the service to your manifest,
  3. In the manifest, at your service, add android:foregroundServiceType="mediaProjection"
  4. Move your media projection code from your activity to the service.
  5. Start the service from your activity which will start the media projection in the service.

Note that foreground services require a persistent notification and a notification channel for API 26 or above.