Recently, I have been looking for reliable ways to control a secured Android Keyguard. Mainly to display a custom lock screen. I know that Google had stated custom lock screens are not officially supported by the platform and should expect things to break, however, with the existing APIs, I believe there must be ways to do this. I have done tons of research for about a week but still having problem here and there. What I have implemented, assuming a secured Keyguard is enabled, so far are,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED allows an activity(a window) to be displayed on screen on, putting the Keyguard behind, and all unsafe actions are prevented. Notification panel is disabled, finishing the activity will bring up the Keyguard. I implemented as following in my lock screen activity.
@Override public void onAttachedToWindow() { window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); }
KeyguardManager
, KeyguardManager.KeyguardLock
are deprecated classes, but they still work all the way to Jelly Bean. To do this, I have a Service
that handles two things, holding a static KeyguardManager
and the related objects, and have it hold a BroadcastReceiver
to receive Intent.ACTION_SCREEN_ON
and Intent.ACTION_SCREEN_OFF
. (all the objects are initialized properly)
For ScreenReceiver
public static synchronized void disableKeyguard() { if ( isLocked ) { if ( keyguardLock == null ) { keyguardLock = keyguardManager.newKeyguardLock(LOG_TAG); } keyguardLock.disableKeyguard(); isLocked = false; } } public static synchronized void reenableKeyguard() { if ( !isLocked ) { if ( keyguardLock == null ) { keyguardLock = keyguardManager.newKeyguardLock(LOG_TAG); } keyguardLock.reenableKeyguard(); keyguardLock = null; isLocked = true; } }
For BroadcastReceiver
@Override public void onReceive( Context context, Intent intent ) { if ( intent.getAction().equals(Intent.ACTION_SCREEN_ON) ) { Intent start = new Intent(context, LockScreen.class); start.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); context.startActivity(start); } else if ( intent.getAction().equals(Intent.ACTION_SCREEN_OFF) ) { ScreenReceiverService.reenableKeyguard(); } }
For LockScreenActivity
, when the user had input the correct passcode,
window.clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); ScreenReceiverService.disableKeyguard(); finish();
Things that works
ACTION_ON
and ACTION_OFF
are received reliably.Things that does not work
Keyguard
and call finish(), my app exits and homescreen or the last activity before the screen went off is shown. However, whenever I press the Home Key, the Keyguard will flash into the screen, quickly dismissing itself immediately, and the normal Home Key function/event is not handled (will not return to homescreen after flashing). This is observed when I rapidly tapped the Home Key repeatedly.I even looked into the Android source code to find out the Home Key handling, but it is never sent to third-party applications unless the window type is WindowManager.LayoutParams.TYPE_KEYGUARD
or WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG
, which will throw SecurityException on 4.0+ even it worked on earlier platforms. And for the Keyguard
, I have declared DISABLE_KEYGUARD
permission use this shouldn't be the problem. My guess is the flag FLAG_SHOW_WHEN_LOCKED
will tell the system to handle to Keyguard
in some ways that would conflict with other disable calls. Since this flag is mostly used for Alarm/SMS type application, which is to show limited information to the user, then dismiss themselves and bring up the Keyguard
. But in my case, having the user unlock my lock screen then unlock the system lockscreen simply defeats the purpose of my app.
So the question is why would the Keyguard
flashes whenever I press Home after I disabled it? Is there any workaround/solution for this issue?
P.S. Thank you for reading such a long question. This is my first time asking a question here, if there is anything that I did wrong, please tell me (i.e. format, grammar, code convention, tags, etc.). Also I had no experience with any programming knowledge, I started with Android before I know what Java is. So I have not taken any proper course/training yet, this community is awesome and often help people like I even if they are simple questions, and of course watching Google I/O videos, reading blogs, read others' code help me a lot. So please tolerate any dumb mistakes/obvious bugs/stupid questions. I am only 16. ^_^"
I have used this with some success in both Gingerbread and ICS to open my activity (via a background service which is starting it). In the activity being started:
@Override public void onAttachedToWindow() { this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON, WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With