Android中Activity的onActivityResult未调用及录屏服务启动失败排查
onActivityResult Not Triggering After Screen Recording Permission Request Hey, let's break down why your onActivityResult method isn't logging anything—this is a classic lifecycle mistake that's easy to miss!
The Core Problem
Look at your onFinish() method: after calling startActivityForResult() to request screen recording permission, you immediately call finish() on your RecordingActivity.
When you finish the activity before the permission dialog is dismissed, the system has no active instance of RecordingActivity to send the result back to. That's why you see the OnFinished log but never the OnActivityResult logs—your activity is already gone by the time the user responds to the permission prompt.
Step-by-Step Fixes
1. Remove the premature finish() call
First, delete the finish() line inside onFinish(). We'll handle finishing the activity after we get the permission result.
Modified onFinish():
@Override public void onFinish() { if (mMediaProjection == null && !isServiceRunning(RecorderService.class)) { // Request Screen recording permission startActivityForResult(mProjectionManager.createScreenCaptureIntent(), ConstKeys.SCREEN_RECORD_REQUEST_CODE); Log.e("RecordingLog","OnFinished"); } else if (isServiceRunning(RecorderService.class)) { // Stop recording if the service is already active // Add your stop logic here if needed } // REMOVE finish() FROM HERE! }
2. Finish the activity in onActivityResult
Once we've handled the permission result (whether granted or denied), we can safely finish the activity. Update your onActivityResult method:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Don't forget this line! Log.e("RecordingLog","OnActivityResult"); if (requestCode == ConstKeys.SCREEN_RECORD_REQUEST_CODE) { if (resultCode == RESULT_CANCELED) { // User denied permission—add a toast or message here if needed finish(); // Finish even if permission is denied return; } // Start the recorder service if permission is granted Intent recorderService = new Intent(this, RecorderService.class); recorderService.setAction(ConstKeys.SCREEN_RECORDING_START); recorderService.putExtra(ConstKeys.RECORDER_INTENT_DATA, data); recorderService.putExtra(ConstKeys.RECORDER_INTENT_RESULT, resultCode); // Note: On Android 8.0+, use startForegroundService instead of startService if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(recorderService); } else { startService(recorderService); } Log.e("RecordingLog","Recorder Service Started"); finish(); // Finish after starting the service } }
3. Fix the onTick logic (bonus improvement)
Your current onTick method manually calls onFinish() when millisUntilFinished <= 800, which can cause the countdown to end early and skip the final tick. Let's clean that up to let the CountDownTimer handle completion naturally:
@Override public void onTick(long millisUntilFinished) { int second = (int) (millisUntilFinished / 1000); txtTesterCounter.setText(App.enToFa(second + "")); }
The CountDownTimer will automatically call onFinish() when the timer reaches zero, so you don't need to force it early.
Why This Works
By keeping RecordingActivity alive until we receive the permission result, the system has a valid target to send the onActivityResult callback to. Once we've processed the result (starting the service or handling denial), we can safely finish the activity.
内容的提问来源于stack exchange,提问作者Jake warton

