2

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

Haris
  • 267
  • 1
  • 2
  • 13

1 Answers1

3

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.

greywolf82
  • 21,813
  • 18
  • 54
  • 108
  • 1
    are you using the emulator? What Android version you are using? It works without problem for me. In addition: it works only for activity with normal theme, i.e. no dialog theme can be applied. – greywolf82 May 17 '14 at 18:25
  • oh, I am testing it on galaxy express and nexus, my code running good on nexus, but not on galaxy, but I am using it on dialog theme :((, let me try with normal theme – Haris May 17 '14 at 18:37
  • I'm sure the problem is the theme. I use the transparent theme and it works. I tried the no display theme and it didn't work. I don't know your use case but maybe you can start a transparent activity and use a dialog bind to it – greywolf82 May 17 '14 at 18:40
  • You were right it was problem due to theme, If you write this in your answer, it would be great too – Haris May 17 '14 at 18:47
  • You mean normal/transparent theme of activity. Is it right? How can I know my activity is normal/transparent or dialog theme. I am working in Android L with Galaxy S4. It does not work now – Jame Nov 23 '16 at 12:06