I want launch my activity on push notification over lock screen without change in lock.
Any special permission for that activity?
I want launch my activity on push notification over lock screen without change in lock.
Any special permission for that activity?
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);
}
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);
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.
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);
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)
}
}