【问题标题】:Android - Is it possible to prevent screen from turning off programatically if user presses power button?Android - 如果用户按下电源按钮,是否可以防止屏幕以编程方式关闭?
【发布时间】:2022-01-11 13:18:24
【问题描述】:

我正在为 Android 平板电脑开发任务关键型应用程序。

我想对应用程序进行万无一失,特别是防止用户在一些需要一些时间的重要过程中关闭应用程序或关闭屏幕。

为了防止用户退出或隐藏应用,有Lock task mode

在 SO 上搜索后,我发现这可能是不可能的 is not really possible - 但是答案来自 2012 年 - 仍然是这种情况吗?

同时,如果检测到ACTION_SCREEN_OFF 意图,我确实实现了打开请求重新打开屏幕的解决方法,描述为here,但它相当难看,而且键盘保护有时被禁用,有时不被禁用,我'不知道为什么。

这是我的代码:

override fun onReceive(context: Context?, intent: Intent) {
            if (intent.action == Intent.ACTION_SCREEN_OFF) {
                Log.i(LOG_TAG, "Screen off was detected, requesting to turning the screen back on...")

                // Disable key lock, so keygoard will not be shown once the screen light back up
                val keyguardManager = getSystemService(KEYGUARD_SERVICE) as KeyguardManager
                keyguardManager.requestDismissKeyguard(this@MainActivity, null)

                // Ask to turn the screen back on - lifted from here
                // https://stackoverflow.com/a/10143686/4574731

                // Ask device to keep screen awake
                val powerManager = getSystemService(POWER_SERVICE) as PowerManager
                val wakeLock = powerManager.newWakeLock(
                    PowerManager.SCREEN_DIM_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP or PowerManager.ON_AFTER_RELEASE,
                    "rpicapp:turnScreenOnReciever"
                )
                wakeLock.acquire(10*1000L /* 10 seconds */)

                try {
                    // Broadcast the ACTION_SCREEN_ON intent after 10 milliseconds
                    val alarmMgr = getSystemService(ALARM_SERVICE) as AlarmManager
                    val screenOnIntent = PendingIntent.getActivity(context, 0, Intent(Intent.ACTION_SCREEN_ON), 0)
                    alarmMgr[AlarmManager.ELAPSED_REALTIME_WAKEUP, 10] = screenOnIntent
                } finally {
                    wakeLock.release()
                }

            }
        }

2021 年是否有更好的解决方法?

谢谢

【问题讨论】:

  • 如果您的平板电脑是三星平板电脑,您可以在需要时使用三星 Knox 禁用该按钮。如果用户按下按钮,他会收到一条 Toast 消息,上面写着“安全策略阻止使用此密钥”或类似的东西。

标签: android android-intent


【解决方案1】:

不幸的是,简单的答案:除非手机已root,否则这是不可能的。

你可以通过只获得 root 权限来做你想做的事,但是,现在拥有 root 手机的用户已经很少了......但这总是取决于你的上下文。如果您提供平板电脑来运行应用程序,您可以对其进行 root 并使用它来实现您的功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多