Palak
Palak

Reputation: 2215

Android, How to launch Activity over Lock screen

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

Any special permission for that activity?

Upvotes: 19

Views: 22044

Answers (5)

Ruslan Zhuravlov
Ruslan Zhuravlov

Reputation: 206

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

Upvotes: 1

Sam
Sam

Reputation: 42377

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.

Upvotes: 4

Vladimir Demirev
Vladimir Demirev

Reputation: 676

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

Upvotes: 33

Camilo Ortegón
Camilo Ortegón

Reputation: 3692

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

Upvotes: 3

Ravi
Ravi

Reputation: 35549

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

Upvotes: 9

Related Questions