Enabling and disabling lock Screen in Android

For enabling or disabling lock screen in Android, we need to get the instance of keyBoard Manager, using getSystemService(). The syntax is as,

KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);

For locking the screen use,


and for disabling the lock use,


This will just hide the lock screen and will display if any incoming call or any such event is happening, it will display that screen. It wont display the lock screen.

When running the application, to disable from going to the lock state, just use
setKeepScreenOn() is set to true. or use the xml attribute android:keepScreenOn="true"

Another way to prevent the device to go to the sleep or lock mode when the application is running is set this api to true - setKeepScreenOn()

And of course we need to give permission android.permission.DISABLE_KEYGUARD


Popular posts from this blog

Open Gallery and get the selected image in appplication

Dismiss or Cancel dialog by swipe gesture

AlarmManager and Notification in Android