0

我正在构建一个屏幕录制应用程序。但它只适用于targetSdkVersion 28,当通过google请求的targetSdkVersion 29时,错误

代码

 @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();
    }

错误

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)

非常感谢!!!!

4

1 回答 1

0

API 29 中添加了一个新的服务属性,显然MediaProjection需要使用前台服务,因为异常说明了这一点。这是您需要做的:

  1. 创建服务,
  2. 将服务添加到您的清单中,
  3. 在清单中,为您服务,添加android:foregroundServiceType="mediaProjection"
  4. 将您的媒体投影代码从您的活动移至服务。
  5. 从您的活动启动服务,这将在服务中启动媒体投影。

请注意,前台服务需要 API 26 或更高版本的持久通知和通知通道。

于 2020-11-04T10:26:03.430 回答