The Context

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);
   
Edit
Report