How to show popup or dialog when phone is locked?

I try to show an action or dialogue when the phone is locked. I tried using WakeLock but it didn’t work, and I can only see activity after my phone is unlocked?

What is the right way to do this?

+5
source share
3 answers

You must use KeyGuardManager to unlock the device automatically, and then acquire a 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.
+4
source

To show activity without releasing the key lock, follow these steps:

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();
+10
source

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

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

To show activity on top of the lock screen or basically remove the lock screen when the action starts, try the following:

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

Both of these options require api 5+

+2
source

All Articles