5

I am trying to show a activity or a dialog when the phone is locked. I have tried using a WakeLock but it did not work and I can only see the activity once my phone is unlocked?

What is the proper way to do this?

Jason
  • 4,004
  • 4
  • 36
  • 62

3 Answers3

10

To show activity without dismissing the keyguard try this:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView();
HarryHao
  • 101
  • 1
  • 3
4

You should use the KeyGuardManager to unlock the device automatically and then acquire your Wake Lock.

    KeyguardManager kgm = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
    boolean isKeyguardUp = kgm.inKeyguardRestrictedInputMode();
    KeyguardLock kgl = kgm.newKeyguardLock("Your Activity/Service name");

    if(isKeyguardUp){
    kgl.disableKeyguard();
    isKeyguardUp = false;
    }

    wl.acquire(); //use your wake lock once keyguard is down.
Dan J
  • 24,995
  • 17
  • 97
  • 170
Donal Rafferty
  • 19,469
  • 37
  • 112
  • 186
2

To show a popup on top of a lock screen try this, from my other answer:

AlertDialog alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.getWindow().setType(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        alertDialog.show();

To show activity on top of a lock screen, or basically remove the lock screen when activity is starts, try this:

public void onCreate(Bundle savedInstanceState){
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
     ...
}

Both of those options require api 5+

Community
  • 1
  • 1
Ilya Gazman
  • 29,832
  • 19
  • 128
  • 206