屏幕录制在安卓 12 或更高版本中每次都会请求权限。

huangapple go评论151阅读模式
英文:

Screen recording asks permission every time in android version 12 or above

问题

谢谢您关注我的问题。

我正在使用Android + Java和Kotlin应用程序。在Android 12或更高版本中,每次需要屏幕录制时都会要求权限。

屏幕录制权限请求

希望这只会在权限请求时问一次。

我注意到XRecorder要求屏幕录制权限只需要一次,而不是每次都要求。难道没有办法让我的应用程序只在权限请求时问一次吗?

希望有人能帮助我解决这个问题。

英文:

Thanks for your attention about my trouble.
I am working with android + java & kotlin app.
It needs to be screen recorded but it asks permission every time in android version 12 or above.

Screen Record Permission Request

屏幕录制在安卓 12 或更高版本中每次都会请求权限。

I hope this would ask only one time for permission request.
I've seen XRecorder does require once for screen recording permission not every time.
Isn't there anyway to make my app to ask only one time for permission request.
Wish someone would help me to solve this.

答案1

得分: 0

根据您提供的内容,以下是翻译的部分:

"In my experience, you can save the result of media projection intent and reuse it during the same application process session. That is how most applications overcome this issue."

根据我的经验,您可以在同一应用程序进程会话期间保存媒体投影意图的结果并重复使用它。这是大多数应用程序克服此问题的方法。

"But be aware that it is more a hack than a solution because the docs do not guarantee it. In contrast, it says you can not reuse it. So use it at your own risk."

但请注意,这更像是一个技巧而不是一个解决方案,因为文档没有保证。相反,文档表示您不能重复使用它。因此,请自担风险使用它。

"From my observations, it will work almost everywhere except some Xiaomi and Huawei devices. You can add them to ignore list and ask every time. Another good idea will be to add a try-catch when reusing an intent, so you can request permission again if the intent is expired."

根据我的观察,它几乎在所有地方都能正常工作,除了一些小米和华为设备。您可以将它们添加到忽略列表中,并每次都询问。另一个好主意是在重复使用意图时添加try-catch,这样如果意图过期,您可以再次请求权限。

"Context: we use this hack in an application with millions of active users, so it has some credibility."

背景信息:我们在一个拥有数百万活跃用户的应用程序中使用这个技巧,因此它具有一定的可信度。

英文:

In my experience, you can save the result of media projection intent and reuse it during the same application process session. That is how most applications overcome this issue.

But be aware that it is more a hack than a solution because the docs do not guarantee it. In contrast, it says you can not reuse it. So use it at your own risk.

From my observations, it will work almost everywhere except some Xiaomi and Huawei devices. You can add them to ignore list and ask every time. Another good idea will be to add a try-catch when reusing an intent, so you can request permission again if the intent is expired.

Context: we use this hack in an application with millions of active users, so it has some credibility.

Code snippet to get the idea, but not production ready:

object MediaProjectionIntentHolder {
	
	var intent: Intent? = null

}

class ScreenRecordingFragment : Fragment() {
	

	// A user clicked button and this function is called
	private fun startScreenRecording() {
		val intent = MediaProjectionIntentHolder.intent
        // You can check Huawei / Xiaomi devices here to ask permission every time
		if (intent != null) {
			recordScreen(intent)
		} else {
			requestPermission()
		}
	}

	private fun recordScreen(intent: Intent) {
		// Actual screen recording start
	}

	private showError() {
		// show error
	}


	private fun requestPermission() {
		val service = requireContext().getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
		val intent = service.createScreenCaptureIntent()

		val screenRecordingSupported = context.packageManager.queryIntentActivities(intent, 0).isNotEmpty()

		if (screenRecordingSupported) {
			startActivityForResult(intent, REQUEST_CODE)
		} else {
			showError()
		}
	}

	override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK && data != null) {
            	MediaProjectionIntentHolder.intent = data
                recordScreen(data)
            } else {
                showError()
            }
        }
    }

	private companion object {
		private consta val REQUEST_CODE = 42
	}
}

huangapple
  • 本文由 发表于 2023年2月19日 23:47:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定