Return to app from lock screen without unlocking with code

Advertisements

On my Android phone, if I am using the Maps app to navigate, and then lock the phone with the lock button, I am able to unlock again and continue navigating without really unlocking my phone (that is, I don’t need to enter my pin, but I also cannot access anything else on my phone before I unlock properly). Is it possible to achieve this behavior in my own apps, and if so, how?

To try to be more clear, this is what I’m hoping to achieve:

  1. User launches app
  2. User hits phone’s lock button. Screen goes off.
  3. User hits phone’s lock button again. Screen goes on, but instead of asking for pin (or whatever people use to secure their phones), phone immediately shows app again. At this point, the user’s only options are to use the app, until he hits the home button, at which point the phone will require unlock code.

Is such behavior possible to implement in my app?

>Solution :

You can use setShowWhenLocked in your Activity (https://developer.android.com/reference/android/app/Activity#setShowWhenLocked(boolean))

Specifies whether an Activity should be shown on top of the lock
screen whenever the lockscreen is up and the activity is resumed.
Normally an activity will be transitioned to the stopped state if it
is started while the lockscreen is up, but with this flag set the
activity will remain in the resumed state visible on-top of the lock
screen. This value can be set as a manifest attribute using
R.attr.showWhenLocked.

Edit: If you also need to support older APIs (taken from here: https://stackoverflow.com/a/50644346/1386873)

if (Build.VERSION.SDK_INT >= 27) {
        setShowWhenLocked(true)
        setTurnScreenOn(true)
} else {
        this.window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
}

Leave a ReplyCancel reply