Java 如何在Android中以编程方式锁定屏幕?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3594532/
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-08-14 02:21:17  来源:igfitidea点击:

How to programmatically lock screen in Android?

javaandroidlockscreen

提问by Mecid

Possible Duplicate:
Lock the android device programatically

可能的重复:
以编程方式锁定 android 设备

How can I programmatically lock the screen in Android?

如何在 Android 中以编程方式锁定屏幕?

回答by YoK

Check this class : com.android.internal.policy.impl.LockScreen

检查这个类:com.android.internal.policy.impl.LockScreen

Referenced from here: Can you lock screen from your app?

从这里引用:你可以从你的应用程序锁定屏幕吗?

Also check code for enabling and disabling lock Screen in Android. (Referenced from here)

还要检查在 Android 中启用和禁用锁屏的代码。(从这里引用)

KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE); 
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE); 

For locking the screen use,

对于锁定屏幕使用,

lock.reenableKeyguard();

and for disabling the lock use,

和禁用锁的使用,

lock.disableKeyguard()

回答by Adam

In order to do this you need to use the Device AdministrationAPI that was added in (I think) 2.2. Once your app is registered on the device as a device administrator, you can then use DevicePolicyManager.lockNow() to lock the screen. The DeviceAdmin sample application in the SDK is a good place to look as well.

为此,您需要使用(我认为)2.2 中添加的设备管理API。一旦您的应用在设备上注册为设备管理员,您就可以使用 DevicePolicyManager.lockNow() 来锁定屏幕。SDK 中的 DeviceAdmin 示例应用程序也是一个很好的查看位置。