I am working on a app in which I have to lock and unlock the screen programmatically. Kindly help ! I have no any idea how to develop this type of functionality which support each version of Android OS.
Asked
Active
Viewed 5.4k times
2 Answers
29
To Unlock
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock kl = km .newKeyguardLock("MyKeyguardLock");
kl.disableKeyguard();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
wakeLock.acquire();

Barend
- 17,296
- 2
- 61
- 80

Viraj Tank
- 1,285
- 11
- 14
-
-
1
-
-
2@Barend Not working on Android 4.4 also What should I do to lock the device? – VVB Oct 08 '15 at 11:23
-
1
-
Does not work with encrypted device running marshmallow, but worked on non-Encrypted device running lollipop. – Thunderstick Dec 12 '16 at 01:52
-
2
-
-
On Android 7.1.1 (OnePlus 3) it sets screen on but doesn't disable (fingerprint protected) screen guard – LLL Mar 24 '18 at 14:06
12
This link might help you solve your Problem :
Unlock and Lock Programmatically
//Get the window from the context
WindowManager wm = Context.getSystemService(Context.WINDOW_SERVICE);
//Unlock
Window window = getWindow();
window.addFlags(wm.LayoutParams.FLAG_DISMISS_KEYGUARD);
//Lock device
DevicePolicyManager mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);

A. Badakhshan
- 1,045
- 7
- 22

Rajeev N B
- 1,365
- 2
- 12
- 24
-
With short content in your link, you can paste here in case that page die. – Trung Nguyen Oct 04 '12 at 08:30
-
1@Amit PL check [this](http://rdcworld-android.blogspot.in/2012/03/lock-phone-screen-programmtically.html) – swiftBoy Jan 16 '13 at 06:54
-
This unlock does not work on 4.4.4, the other answer worked. Though with tons of depreciated calls. – catalyst294 Aug 10 '14 at 00:59
-