簡體   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