繁体   English   中英

使用 onActivityResult 而不是 startActivityFOrResult

[英]use onActivityResult instead of startActivityFOrResult

private void toggleScreenShare(View v) {
    ToggleButton toggleButton=(ToggleButton)  v;
    if(toggleButton.isChecked()){
        initRecorder();
        recordScreen();
        
    }
    else {
        mMediaRecorder.stop();
        mMediaRecorder.reset();
        stopRecordScreen();

        mVideoView.setVisibility(View.VISIBLE);
        mVideoView.setVideoURI(Uri.parse(mVideoUrl));
        mVideoView.start();
    }
}

private void recordScreen() {
    if(mediaProjection==null){
       ** startActivityForResult(mMediaProjectionManager.createScreenCaptureIntent(),REQUEST_CODE);**
        
        return;
    }

    mVirtualDisplay=createVirtualDisplay();
    mMediaRecorder.start();
}

这是我的代码,我想在粗体中使用 onActivityResult,因为不推荐使用 StartActivityFOrResult,谁能帮我写这个

我试了很多次,想要代码

这部分代码将为您提供意图的结果。

/**
 * In your activity
 * this will get you the result from the other intent.
 * I do not know, what exactly you are exactly doing there.
 * So you no longer need technically the request code.
 */
private final ActivityResultLauncher<Intent> yourLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
    @Override
    public void onActivityResult(ActivityResult result) {
        Intent intentData = result.getData();

        if (intentData == null) return;

        intentData.getBooleanExtra("BOOLEAN", false);
    }
});

这部分代码将以您想要的意图调用您的启动器。

yourLauncher.launch(mMediaProjectionManager.createScreenCaptureIntent());

希望这个简单的例子能启发方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM