Stepan Sanda
Stepan Sanda

Reputation: 2340

Oreo (8.1) cannot start activity on lock screen

I'm working on NFC payment application. The payment is possible when screen is on even when device is locked. Basically same behaviour as Android Pay. After payment I want to display victory screen to the user to inform him about the payment result - done, error, pin request etc..

I added this to the manifest

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

And in onCreate I'm setting this flags

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
            | WindowManager.LayoutParams.FLAG_FULLSCREEN
            | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);

And it is working fine on most devices. But once I updated Nexus 5X to Android 8.1 it stopped working. The NFC transaction is done on background without any issue, but the victory screen is not displayed at all when my phone is on lock screen with screen turned on. When 5X is unlocked it starts the activity without problem. Then I found that it is not working only when the app is not running in background (when I go to the settings and force stop my app). When I open the app and leave it in background, then lock my phone and turn screen on - it is working. But when the app is not running at all - it will not display my activity. On other devices like Samsung S8 with Andorid 7.0 its working even when I force stop my activity and lock the phone.

I tested Android Pay on 5X with Android 8.1 and it is working on lock screen. So it is still possible to start activity on lock screen, but I'm probably missing something there.

Thank you in advanced.

Upvotes: 6

Views: 3362

Answers (1)

Benjamin
Benjamin

Reputation: 7368

On Android 8.1 (API 27), WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED and WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON have been deprecated.

You should now add the following to your activity entry inside your manifest:

android:showWhenLocked="true"
android:turnScreenOn="true"

Upvotes: 12

Related Questions