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.
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
However, it is not giving me any result and not even giving me an开发者_JS百科y error or exception.
What am I missing?
If your minimum supported OS is 2.2 then you can use the DevicePolicyManager lockNow() method.
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.
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
精彩评论