how to show popup Activity or Dialog when phone is locked?

前端 未结 3 1777
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-03 04:36

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 unloc

相关标签:
3条回答
  • 2021-01-03 05:17

    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();
    
    0 讨论(0)
  • 2021-01-03 05:23

    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+

    0 讨论(0)
  • 2021-01-03 05:25

    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.
    
    0 讨论(0)
提交回复
热议问题