18

I want launch my activity on push notification over lock screen without change in lock.

Any special permission for that activity?

Insane Skull
  • 9,220
  • 9
  • 44
  • 63
Palak
  • 2,165
  • 2
  • 21
  • 31

5 Answers5

33
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1)
    {
        setShowWhenLocked(true);
        setTurnScreenOn(true);
        KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        if(keyguardManager!=null)
            keyguardManager.requestDismissKeyguard(this, null);
    }
    else 
    {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    }
  • 1
    Actually, this code looks like the best and version-aware solution. should be the accepted answer. – Grisgram Sep 23 '19 at 09:46
  • 1
    Where should this code go? I had it in onCreate, but when I turn the screen off, the onCreate method is never called, so this code is never run. – trampster Apr 07 '20 at 23:53
  • @trampster This code simply sets flags for your app. These describe how the app should behave when phone is locked. The code is not supposed to run on lock/unlock. – Vladimir Demirev Apr 14 '20 at 14:27
  • 1
    why we need `keyguardManager.requestDismissKeyguard(this, null);` ? it actually causes memory leak – beginner Nov 23 '20 at 11:25
  • @beginner The Keyguard deals with unlocking the phone and will prompt you to input a PIN or pattern, whatever security measure you have. When you display info on a locked screen - it is your choice if you want that or not. About the method itself, here is a description at android.com: https://developer.android.com/reference/android/app/KeyguardManager#requestDismissKeyguard(android.app.Activity,%20android.app.KeyguardManager.KeyguardDismissCallback) – Vladimir Demirev Nov 28 '20 at 22:49
  • @VladimirDemirev can you please confirm if this works on Android 8 in this scenario: App is turned on, then phone is locked, then does it wake up to show on top of lockscreen? For me it doesn't wakeup on push when I turn on the app. In Android 10 this code will work. – Waleed Jan 04 '21 at 11:20
  • @Waleed I do have the same issue. I start my activity in a service (which is triggered from push). The think is, that my Pixel phone will wake up, but not my Samsung A20. We tested 4 Android devices and on 2 it's working and the other don't react. Did you fix your problem? – mars3142 Jun 17 '21 at 09:44
9

After API level 17 this would work

<activity
    android:name=".yourActivityName"
    android:showOnLockScreen="true"
    android:screenOrientation="sensorPortrait" >

or write this in onCreate() before calling setContentView()

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); 
Ravi
  • 34,851
  • 21
  • 122
  • 183
4

The other answers include extra functionality that you may or may not want.

The minimum code to allow your activity to display on the lock screen is this:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (Build.VERSION.SDK_INT >= 27)
        setShowWhenLocked(true);
    else
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

    // setContentView etc
}

If you also want your activity to turn on the screen (since it might be off) or unlock the keyguard, then see Vladimir Demirev's answer.

Sam
  • 40,644
  • 36
  • 176
  • 219
  • 1
    can you please confirm if this works on Android 8 in this scenario: App is turned on, then phone is locked, then does it wake up to show on top of lockscreen? – Waleed Jan 04 '21 at 11:20
  • You code will be more readable if you use a constant for the build number instead of a hardcoded value such as 27. – Peter Chaula May 24 '22 at 20:06
3

In the method onCreate(Bundle savedInstanceState) you should add some window flags:

Window window = this.getWindow();
window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
Camilo Ortegón
  • 3,414
  • 3
  • 26
  • 34
0

To show your activity over lock screen:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
        
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
        setShowWhenLocked(true)
        setTurnScreenOn(true)
    } else {
        window.addFlags(
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
        )
    }

    setContentView(binding.root)
    ...
}

Please note that Xiaomi requires additional permission from the manufacturer. You can read about this issue here: https://stackoverflow.com/a/52590297/11980977

Additionaly, if you want to request unlock keyguard and perform action when keyguard unlocked:

private fun handleButtonClickWhenLocked(clickAction: () -> Unit) {
    val keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
    if (!keyguardManager.isKeyguardLocked) {
        clickAction.invoke()
    } else {
        val keyguardDismissCallback = object : KeyguardManager.KeyguardDismissCallback() {

             override fun onDismissSucceeded() {
                 clickAction.invoke()
             }
         }
         keyguardManager.requestDismissKeyguard(this, keyguardDismissCallback)
    }
}