How to display Activity when the screen is locked?

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*/
}

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.


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(); 

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