Device : (Samsung Android 14 One UI 6) && Pixel 6 enter image description here Issue: the media projection permission dialog is shown again & again in the above-mentioned device. but it was working fine with other devices. I have no idea how to solve this issue.
I have used this code.Pls check
private fun setupMediaIntent() {
try {
if (!screenshotUtil.isMediaProjectionEnabled()) {
val mediaProjectionManager =
context?.getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
startForResult.launch(mediaProjectionManager.createScreenCaptureIntent())
} else {
handleNavigation()
}
} catch (exx: Exception) {
exx.printStackTrace()
}
}
private val startForResult =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { activityResult ->
try {
if (activityResult.resultCode == Activity.RESULT_OK) {
screenshotUtil.setResultCode(activityResult.resultCode)
screenshotUtil.setMediaProjectionIntent(activityResult.data)
ServiceUtils.restartService(
requireContext(),
ServiceType.AUTO_SCREENSHOTS.value
)
handleNavigation()
} else {
setupMediaIntent()
}
} catch (exx: Exception) {
exx.printStackTrace()
}
}
private fun handleNavigation() {
PreferencesUtils.setPreference(requireContext(), SETUP_FINISHED, "true")
PreferencesUtils.setRestartVirtualDisplay(requireContext(), "false")
if (initialSetup || fromDashboard) {
LogUtil.writeLog(
requireContext(),
"Initial Permission",
"Media Projection Permission Enabled"
)
findNavController().popBackStack(R.id.mainFragment, false)
} else {
activity?.finishAffinity()
}
Constants.finishes = true
}
Excepting Behavior : the permission dialog should not popup again & again one it enabled
if "isMediaProjectionEnabled()" returns TRUE you have to provide same "activityResult.data" as before, or the Dialog will be shown. It's a strange behaviour but that's it.