Android screen lock/ unlock programmatically [duplicate]
Asked Answered
N

2

24

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.

Nicaea answered 4/10, 2012 at 8:24 Comment(2)
Check out this post. It may help you. [Lock/Unlock Screen][1] [1]: https://mcmap.net/q/372695/-how-to-display-activity-when-the-screen-is-lockedCapitalization
check github.com/amirarcane/lock-screenAlmost
M
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();
Madlin answered 7/11, 2013 at 13:8 Comment(8)
worked for me. but deprecated method warnings appear.Corinnacorinne
Worked like Zalo app, Viber app ... Really good.Rabassa
@Barend Not working on Android 4.4 also What should I do to lock the device?Candelariacandelario
Both are depreciated.Dioptrics
Does not work with encrypted device running marshmallow, but worked on non-Encrypted device running lollipop.Perchloride
Doesn't work on Nougat (7.0) with secure screen lock.Hornbook
Very very nice.Linalinacre
On Android 7.1.1 (OnePlus 3) it sets screen on but doesn't disable (fingerprint protected) screen guardLorenzoloresz
R
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);
Riant answered 4/10, 2012 at 8:28 Comment(4)
With short content in your link, you can paste here in case that page die.Liebfraumilch
@Nicaea PL check thisBogus
This unlock does not work on 4.4.4, the other answer worked. Though with tons of depreciated calls.Compare
non of these answer work for 4.4Rohrer

© 2022 - 2024 — McMap. All rights reserved.