编程锁定屏幕在Android [英] Lock the screen programmatically on Android

查看:194
本文介绍了编程锁定屏幕在Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从我的应用程序编程锁定屏幕。

I have to lock the screen programatically from my application.

我使用了下列code:

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设置以下的Andr​​oidManifest权限。

I set following permission in the 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.

我在想什么?

推荐答案

如果您的最低支持的操作系统是2.2,那么你可以使用<一个href="http://developer.android.com/reference/android/app/admin/DevicePolicyManager.html#lockNow%28%29">DevicePolicyManager lockNow()方法。

If your minimum supported OS is 2.2 then you can use the DevicePolicyManager lockNow() method.

,尝试寻找在ApiDemos样品code中的DeviceAdminSample。 Android SDK中附带了所有的样品,因此您可以轻松地添加ApiDemos作为一个Eclipse项目,并在模拟器中运行它。

If you want to know more about the DevicePolicyManager interface, try looking at the DeviceAdminSample in the ApiDemos sample code. 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.

如果你需要支持旧版本的操作系统也一样,看到我的问题在这里:
<一href="http://stackoverflow.com/questions/5811761/using-os-2-2-devicepolicymanager-sdk-classes-on-android-whilst-supporting-os-2-1">Using OS 2.2 DevicePolicyManager SDK类在Android上同时支持OS 2.1的设备

If you need to support older OS versions too, see my question here:
Using OS 2.2 DevicePolicyManager SDK classes on Android whilst supporting OS 2.1 devices

这篇关于编程锁定屏幕在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆