Monday, October 17, 2011

Locking and unlocking an android device programmatically

 
How to unlock and lock a screen
Back pocket code snippet
*Note*
For the following to work you need reference to an activity that can talk to the window object.
   1: //Get the window from the context
   2: WindowManager wm = Context.getSystemService(Context.WINDOW_SERVICE);
   3:  
   4: //Unlock
   5: //http://developer.android.com/reference/android/app/Activity.html#getWindow()
   6: Window window = getWindow();  
   7: window.addFlags(wm.LayoutParams.FLAG_DISMISS_KEYGUARD);  
   8:  
   9: //Lock device
  10: DevicePolicyManager mDPM;
  11: mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);