Search code examples
javaandroidkeyguardlock

Android How to disable pattern lock


I want to disable screen lock. show screen and after dismissing it, i want to lock screen again, for this purpose I am using this code.

after onCreate()

PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "INFO");


    KeyguardManager km = (KeyguardManager)  this.getSystemService(Context.KEYGUARD_SERVICE);
    kl = km .newKeyguardLock("MyKeyguardLock");
    kl.disableKeyguard();

and on dismissing screen I am using, kl.reenableKeyguard() to lock screen again.

This is working absolutely great if I am using swipe screen lock, but if I am using pattern lock, This code is not working. I know it is possible, there are apps doing this, but so far I am unable to find a way out.

Edit : I found this code is working in nexus, but not on galaxy


Solution

  • You could create an activity to start when needed with:

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                            | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
                    WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                            | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        }
    

    Wakelocks are deprecated and only the window flags should now be used.

    Edit: It works only with normal/transparent theme, it doesn't work with dialog theme.