How to display Activity when the screen is locked?

后端 未结 4 1323
慢半拍i
慢半拍i 2020-11-27 13:57

My application is launched on car docking event, I want to wake up phone (done by system) and unlock screen when I plug my device.

Is it posssible ?

相关标签:
4条回答
  • 2020-11-27 14:16

    Use Activity.getWindow() to get the window of your activity; use Window.addFlags() to add whichever of the following flags in WindowManager.LayoutParams that you desire: FLAG_DISMISS_KEYGUARD, FLAG_SHOW_WHEN_LOCKED, FLAG_TURN_SCREEN_ON

    This is how the standard car dock (and desk dock) app implements this behavior.

    0 讨论(0)
  • 2020-11-27 14:18

    You will be able to use FLAG_DISMISS_KEYGUARD only for phones which do not have security enabled locks like pattern lock.

    FLAG_SHOW_WHEN_LOCKED will only bring your current Activity on the top, if user tries to move elsewhere, he will have to unlock the screen.

    Alternatively, you can add permission in your manifest:

    <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
    

    And, in your activity on create:

    KeyguardManager manager = (KeyguardManager) this.getSystemService(Context.KEYGUARD_SERVICE);
    KeyguardLock lock = manager.newKeyguardLock("abc");
    lock.disableKeyguard(); 
    
    0 讨论(0)
  • 2020-11-27 14:19

    I'm use for upping activity to top level

        private Window wind;
        @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        /******block is needed to raise the application if the lock is*********/
        wind = this.getWindow();
        wind.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
        wind.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        wind.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
        /* ^^^^^^^block is needed to raise the application if the lock is*/
    }
    
    0 讨论(0)
  • 2020-11-27 14:21

    When using a lock pattern or a pin entry I needed to add the following as well because the screen turned off in less than 5 seconds:

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    
    0 讨论(0)
提交回复
热议问题