Home > database >  How to use onActivityResult in Android
How to use onActivityResult in Android

Time:09-22

In my application I want to use onActivityResult in Activity and for this I write below codes.
But after call onActivityResult show me logs for 2 times and show error result!
I used MediaProjectionManager and click on Allow permission, but why show cancel request code?
My codes :

    private CountDownTimer reverseTimer() {
        return countDownTimer = new CountDownTimer(2 * 1000, 1000) {
            @SuppressLint("SetTextI18n")
            @Override
            public void onTick(long millisUntilFinished) {
                int second = (int) (millisUntilFinished / 1000);
                if (millisUntilFinished < 1100) {
                    onFinish();
                } else {
                    txtTesterCounter.setText(App.enToFa(second   ""));
                }
            }

            @Override
            public void onFinish() {
            MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
                Intent permissionIntent = mediaProjectionManager != null ? mediaProjectionManager.createScreenCaptureIntent() : null;
                startActivityForResult(permissionIntent, ConstKeys.SCREEN_RECORD_REQUEST_CODE);
                Log.e("ResultLog", "Request : "   ConstKeys.SCREEN_RECORD_REQUEST_CODE);
            }
        };
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //The user has denied permission for screen mirroring. Let's notify the user
        Log.e("ResultLog", "Request : "   resultCode   "---"   "Request : "   requestCode);
        if (resultCode == RESULT_CANCELED && requestCode == ConstKeys.SCREEN_RECORD_REQUEST_CODE) {
            Toast.makeText(this, "Access failed!", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
}

Logcat messages :

2022-09-21 19:16:46.948 10083-10083/com.myapp E/ResultLog: Request : 1003
2022-09-21 19:16:47.908 10083-10083/com.myapp E/ResultLog: Request : 1003
2022-09-21 19:16:48.276 10083-10083/com.myapp E/ResultLog: Request : 0---Request : 1003
2022-09-21 19:16:48.349 10083-10083/com.myapp E/ResultLog: Request : -1---Request : 1003

Why call log codes 2 times and not allow permission ?

CodePudding user response:

Do not call onFinish() yourself.

It will automatically be called when... all has finished..

CodePudding user response:

its calling it first time when your condition is returning true and second time when its actually finished by itself , Means the timer is not being stopped or cancelled when your condition is true, to make it call once you need to stop the timer under the onFinish(); method there is method called cancel() to stop the timer try with your object name in your case its countDownTimer so it should look something like countDownTimer.cancel() under the onFinish()

let me know if that solved the issue.

  • Related