How to programmatically lock screen in Android? [duplicate]
Asked Answered
B

2

15

Possible Duplicate:
Lock the android device programatically

How can I programmatically lock the screen in Android?

Barns answered 29/8, 2010 at 11:28 Comment(0)
I
15

Check this class : 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)

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()
Intuition answered 29/8, 2010 at 11:34 Comment(4)
i test this code,but this not workingBarns
Can you share code what you tried? and What issues you are facing with it ?Intuition
This code will not work on SDK 8 and above do use Adams suggestion on using device policy manager.Biceps
This does not work for meEgide
A
4

In order to do this you need to use the Device Administration API 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.

Atwekk answered 14/1, 2011 at 19:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.