使用onActivityResult代替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());

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

相关内容

  • 没有找到相关文章

最新更新