Skip to content Skip to sidebar Skip to footer

Start Activity Even When Screen Is Turned Off

I'm trying to start an activity with an alarm. The PendingIntent starts a receiver and the receiver starts the activity. My current issue is that the activity starts in the backgro

Solution 1:

You should have in your AndroidManifest.xml

<activityandroid:name=".AlarmActivity"android:showOnLockScreen="true"android:turnScreenOn="true"/>

Also the following check should be after the setContentView(). Since at the time you adding the flags there is not view that can make use of them.

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_DISMISS_KEYGUARD
           or WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
           or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
           or WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON)
}

Post a Comment for "Start Activity Even When Screen Is Turned Off"