Media projection permission dialog popup again & again in (Samsung Android 14 One UI 6) && Pixel 6 (Android14)

332 views Asked by At

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

3

There are 3 answers

0
emandt On

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.

1
xueliang nie On

Don't release MediaProjection, VirtualDisplay, ImageReader, repeat Use mImageReader.acquireLatestImage().

5
Droid Automations On

Also, make sure you don't shut down the surface at any point on android 14+ before you are completely done with media projection, had that issue myself. Couldn't find why my service didn't work, then discovered I had shut down the surface immediately after capturing a valid frame. By leaving the surface, image reader, and virtual display active, you'll be able to just use something like this repeatedly. Beware that this approach is constantly consuming resources as well!

 val image = imageReader.acquireNextImage()
        if (image != null) {
            val planes = image.planes
            val buffer = planes[0].buffer
            val pixelStride = planes[0].pixelStride
            val rowStride = planes[0].rowStride
            val rowPadding = rowStride - pixelStride * width
            val bitmapWidth = width + rowPadding / pixelStride
            if (latestBitmap == null || latestBitmap!!.width != bitmapWidth || latestBitmap!!.height != height) {
                latestBitmap?.recycle()
                latestBitmap = Bitmap.createBitmap(bitmapWidth, height, Bitmap.Config.ARGB_8888)
            }
            latestBitmap?.copyPixelsFromBuffer(buffer)
            image.close()