sajjoo
sajjoo

Reputation: 6614

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 any error or exception.

What am I missing?

Upvotes: 4

Views: 7344

Answers (1)

Dan J
Dan J

Reputation: 25673

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

Upvotes: 6

Related Questions