Prakhar Gupta
Prakhar Gupta

Reputation: 131

Disable lock Screen in Android 21 and above

I am trying to disable lock screen while making my screen turn off but as soon as screen turns off lock is held again.

I am using below code to disable the lock screen:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
} else {
    KeyguardManager km = (KeyguardManager) activity.getSystemService(KEYGUARD_SERVICE);
    kl = km.newKeyguardLock("name");
    kl.disableKeyguard();
}

And to enable the lock screen:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
} else {
    KeyguardManager km = (KeyguardManager) activity.getSystemService(KEYGUARD_SERVICE);
    kl = km.newKeyguardLock("name");
    kl.reenableKeyguard();
}

Upvotes: 1

Views: 559

Answers (2)

Harsh Jain
Harsh Jain

Reputation: 1372

Try this in OnCreate of the Activity.

 Window win = getWindow();
    win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

Upvotes: 2

Janvi Vyas
Janvi Vyas

Reputation: 752

Please give below permission also in android.manifest.xml

<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>

You can also refer this link for more info

https://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/

Upvotes: 0

Related Questions