java Android - 激活系统键锁(又名锁屏)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/733721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:34:01  来源:igfitidea点击:

Android - Activation of the system key lock (aka lock screen)

javaandroidkeyboardlockingdevice-admin

提问by Johannes Weiss

I have to activate android's system key lock (the one you get when you press the power off/hang upbutton). See here:

我必须激活 android 的系统键锁(当你按下电源关闭/挂断按钮时你得到的那个)。看这里:

img

图片

I already browsed the docs but everything I found was PowerManagerand KeyguardManager. Both seem not to be the solution :-(.

我已经浏览了文档,但我发现的一切都是PowerManagerKeyguardManager。两者似乎都不是解决方案:-(。

So, does everyone know how to achieve that from a android application? (If special permissions are required, that is no problem but changing the device's settings is not a solution...)

那么,每个人都知道如何从 android 应用程序实现这一目标吗?(如果需要特殊权限,那没问题,但更改设备的设置不是解决方案......)

EDIT: Or does someone know that this is definitely not possible at all? Btw. craigs solution with sending keys does not work anymore (see comments).

编辑:或者有人知道这绝对不可能吗?顺便提一句。带有发送密钥的 craigs 解决方案不再起作用(见评论)。

采纳答案by Orkun Ozen

I have been searching for an answer to the exact same question for a while. Apparently, after 2.0 onwards the device manager privileges for the app level were removed. But with Froyo - 2.2 the device policy manager is revealed granting us developers a multitude of administrative level controls.

一段时间以来,我一直在寻找完全相同问题的答案。显然,在 2.0 之后,应用程序级别的设备管理器权限被删除。但是在 Froyo - 2.2 中,设备策略管理器被揭示为我们的开发人员提供了大量的管理级别控制。

http://developer.android.com/guide/topics/admin/device-admin.html

http://developer.android.com/guide/topics/admin/device-admin.html

回答by sthg

What you're looking for is the reenableKeyguard()method in KeyguardManager.KeyguardLock my friend !

您正在寻找的是KeyguardManager.KeyguardLock 我的朋友中的 reenableKeyguard ()方法!

回答by JRL

Looks like the screen lock function is performed using the method:

看起来屏幕锁定功能是使用以下方法执行的:

public void goToSleep(long time)

method in PowerManager.java. It's possible to get a reference to it in this fashion:

中的方法PowerManager.java。可以通过这种方式获得对它的引用:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

However this requires the permission

但是这需要许可

android.permission.DEVICE_POWER

which is a level 2 permission available to the system only.

这是仅对系统可用的 2 级权限。

So looks like this isn't doable. This is for version 1.1 only, I don't know for 1.5.

所以看起来这是不可行的。这仅适用于 1.1 版,我不知道 1.5 版。

回答by Craig Otis

There's a pretty good example here:

这里有一个很好的例子:

http://www.anddev.org/throwing-simulating_keystrokes_programatically-t717.html

http://www.anddev.org/throwing-simulating_keystrokes_programatically-t717.html

It looks like you can programmatically cause any keystroke to be sent to the system. It sounds like the keycode you're looking for is the KEYCODE_ENDCALL, but if that doesn't work there are plenty of other codes to try here:

看起来您可以通过编程方式将任何击键发送到系统。听起来您正在寻找的键码是 KEYCODE_ENDCALL,但如果这不起作用,还有很多其他代码可以在这里尝试:

http://developer.android.com/reference/android/view/KeyEvent.html

http://developer.android.com/reference/android/view/KeyEvent.html

I don't know if there's any API call to cause the lock to occur, but this seems like a pretty sturdy workaround until you find a better solution.

我不知道是否有任何 API 调用导致锁定发生,但这似乎是一个非常可靠的解决方法,直到您找到更好的解决方案。

回答by Steve Pomeroy

Digging through the Android source found WindowManagerService which seems to have a public method (startAppFreezingScreenLocked) for activating this. This may be a good place to start looking for your answer, as unfortunately it doesn't seem as though you can directly get a WindowManagerService object.

挖掘 Android 源代码发现 WindowManagerService 似乎有一个公共方法 ( startAppFreezingScreenLocked) 来激活它。这可能是开始寻找答案的好地方,因为不幸的是,您似乎无法直接获取 WindowManagerService 对象。