Display app while the screen is locked

You can easily disable keyguard from your application.

All you need is two lines of code :

1. Get the window instance
Window w = getWindow(); // from OnCreate of activity

2. set the flag – FLAG_SHOW_WHEN_LOCKED
w.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

This will display the activity and then on back it will enable the lock again.

But if we require to disable the keyguard meaning it will not lock after that activity is displayed, we need to follow the below approach

1. Create an instance of Keyguard manager
KeyguardManager mKeyGuardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
2. Create a new lock
KeyguardLock newKeyguardLock = mKeyGuardManager.newKeyguardLock("screenunlock");
3. Disable the lock
newKeyguardLock.disableKeyguard();

To enable it back just follow the below:
newKeyguardLock.reenableKeyguard();