简体   繁体   English

在Android上以编程方式锁定屏幕

[英]Lock the screen programmatically on Android

I have to lock the screen programatically from my application. 我必须以编程方式从我的应用程序中锁定屏幕。

I have used the following code: 我使用了以下代码:

String service = Activity.KEYGUARD_SERVICE;
KeyguardManager mgr = (KeyguardManager)getSystemService(service);
KeyguardLock lock = mgr.newKeyguardLock(KEYGUARD_SERVICE);
lock.reenableKeyguard();

I set following permission in the AndroidManifest. 我在AndroidManifest中设置了以下权限。

 <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

However, it is not giving me any result and not even giving me any error or exception. 但是,它没有给我任何结果,甚至没有给我任何错误或例外。

What am I missing? 我错过了什么?

If your minimum supported OS is 2.2 then you can use the DevicePolicyManager lockNow() method. 如果您支持的最低操作系统是2.2,那么您可以使用DevicePolicyManager lockNow()方法。

If you want to know more about the DevicePolicyManager interface, try looking at the DeviceAdminSample in the ApiDemos sample code. 如果您想了解有关DevicePolicyManager接口的更多信息,请尝试查看ApiDemos示例代码中的DeviceAdminSample。 The Android SDK comes with all the samples so you can easily add the ApiDemos as an Eclipse project and run it in the simulator. Android SDK附带了所有示例,因此您可以轻松地将ApiDemos添加为Eclipse项目并在模拟器中运行它。

If you need to support older OS versions too, see my question here: 如果您还需要支持较旧的OS版本,请在此处查看我的问题:
Using OS 2.2 DevicePolicyManager SDK classes on Android whilst supporting OS 2.1 devices 在Android上使用OS 2.2 DevicePolicyManager SDK类,同时支持OS 2.1设备

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM