How to Disable Keyguard and display an activity to the user when receiver of SCREEN_ON is triggered?
Asked Answered
E

3

8

How can I disable the keyguard when a broadcast receiver is activated by screen_on, so that when it occurs the user sees an activity that I have started behind it? (The activity is running already...)

I have been trying the following code from a broadcast receiver triggered by screen off...

KeyguardManager  myKeyGuard = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
myLock = myKeyGuard.newKeyguardLock();
myLock.disableKeyguard();

It doesn't seem to be working though as it is. When I turn the screen on, I still have to manually unlock the keyguard on the phone to reveal the activity behind it.

Exclusive answered 20/11, 2010 at 18:1 Comment(2)
I've come across some code that has used exitKeyguardSecurely() in addition to this. I have not been able to get it to work yet though so I am now setting the flag FLAG_DISMISS_KEYGUARD for my activity.Exclusive
Ryan, can you please share your code. I too working on the same.Dram
E
1

I think I figured out what I did wrong. It seems like it was just a tag error in my manifest when using the disable_keyguard permission. It's working correctly now that I corrected the manifest.

Exclusive answered 23/11, 2010 at 14:12 Comment(1)
adding this permission doesnt work for me. I need to run my activity when the user press the unlock button. The activity should trigger before the unlock screen.Dram
P
5

i would recommend using the window flags dismiss_keyguard or show_when_locked if you have a window that needs to come over the top of the lockscreen right at wakeup.

http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_SHOW_WHEN_LOCKED

how you use this is as follows (called in onCreate before setting layout)

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
Panchito answered 13/2, 2011 at 6:24 Comment(3)
I know this is an old post, but the flags did not work for me no matter where I put them, the keyguard manager on the other hand always seems to work. Just a point to note for other readersSaphra
It's true that using the flags is the 'recommended' solution, however, you'll find that they are far from perfect in real-life usage. For example, with flags android will briefly 'flash' the keyguard when moving the activity out of focus. It's minor, but grating. Disabling the keyguard completely doesn't have that issue.Bordello
Be aware that the Activity can't be transparent at all. Also, you can't use the dialog.getWindow() to set the flags either. This is probably to not allow an application to partially 'show' activities from other applications. See this issue code.google.com/p/android/issues/detail?id=8778Reptant
E
1

I think I figured out what I did wrong. It seems like it was just a tag error in my manifest when using the disable_keyguard permission. It's working correctly now that I corrected the manifest.

Exclusive answered 23/11, 2010 at 14:12 Comment(1)
adding this permission doesnt work for me. I need to run my activity when the user press the unlock button. The activity should trigger before the unlock screen.Dram
D
1

I'm not seeing the Keyguard solution work (and it's been deprecated). What works for me is to grab a permanant PARTIAL_WAKE_LOCK that I never release. This prevents the device from truly falling asleep and thus avoids the lock-screen every being activated.

Then every time the screen shuts off, I listen for that notification and wake the device back up. It works on all the devices I have tested on.

I realize that this will kill the device's battery pretty fast, so, you have to use this very sparingly and mindfully.

Deceitful answered 28/3, 2014 at 0:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.