【问题标题】:use onActivityResult instead of startActivityFOrResult使用 onActivityResult 而不是 startActivityFOrResult
【发布时间】:2022-11-30 20:26:56
【问题描述】:
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 已被弃用,谁能帮我写这个

我试了很多次,想要代码

【问题讨论】:

    标签: java android onactivityresult startactivityforresult


    【解决方案1】:

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

    /**
     * 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());
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      • 2012-08-27
      • 1970-01-01
      • 2012-06-07
      • 1970-01-01
      相关资源
      最近更新 更多