5

As title, is it possible for me to wake my phone up and skip the screenlock? I want to write an app based on the alarm clock, when it's time, the app should do detect something in the background even when the phone is sleeping. When something is detected, the phone should automatically wake up, skip the screenlock, and show a picture(start a new activity ). Can anybody give me some suggestions or examples on these? Thanks a lot!

Jim31837
  • 1,589
  • 3
  • 12
  • 12

2 Answers2

4

The activity you're starting needs one or both of the following layout parameters.

Use FLAG_SHOW_WHEN_LOCKED so that the activity appears above the screenlock.

Use FLAG_DISMISS_KEYGUARD to automatically dismiss the keyguard.

More details at http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html.

Nik
  • 2,380
  • 2
  • 16
  • 15
0

Below Kotlin code skips the lock screen for the activity it is applied on:

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
    val keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
    keyguardManager.requestDismissKeyguard(this, null)
    setShowWhenLocked(true)
} else {
    window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD)
}

For more elaborate explanation on keyguard flags, go here.

lomza
  • 9,412
  • 15
  • 70
  • 85