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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Amit
  • 717
  • 1
  • 6
  • 18

2 Answers2

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
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