I am working in an app where I am taking screenshots using Media Projection API. I am able to successfully get the screenshot of the user foreground screen but unfortunately am not able to get the state whether user has already provided Screen capturing permission or not.
Requirement: I need to show the permission window only once when the user has not provided Screen capture permission.
Current Behaviour: Each time the permission window is getting displayed while launching the app. Please find the screenshot below
Code that I am using to show the permission window in MainActivity:
MediaProjectionManager mgr = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
startActivityForResult(mgr.createScreenCaptureIntent(),
Constants.RESULT_CODE.SCREENSHOT);
And after if the user choose YES from the permission window, I am starting a Service as below:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Constants.RESULT_CODE.SCREENSHOT) {
if (resultCode == RESULT_OK) {
Intent intent = new Intent(this, WindowChangeDetectingService.class);
intent.putExtra(Constants.INTENT_KEY.SCREENSHOT, resultCode);
intent.putExtra(Constants.INTENT_KEY.SCREENSHOT_DATA, data);
startService(intent);
finish();
}
}
}
Expected Output:
I am figuring out the solution to check the simple permission check which tells whether the permission has already provided or not using any boolean check before requesting for the Media Projection Services permission window something like
if(isMediaPermissionAlreadyEnabled){
MediaProjectionManager mgr = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
startActivityForResult(mgr.createScreenCaptureIntent(),
Constants.RESULT_CODE.SCREENSHOT);
}
I have been struggling hard to get the solution for Permission check since 3 days and I am not able to find any work around to solve my use case. It would be really helpful if someone provide me some suggestions or tips to resolve my issue. Thanks in advance.