Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 573
Stepan Sanda Avatar asked Jan 23 '18 15:01

Stepan Sanda


People also ask

How do I show activity on lock screen instead of notification?

Adding the Activity to Notification We need to create another PendingIntent that'll start the activity that'll be shown on the lock screen. We then pass that intent to setFullScreenIntent . It's also important to add a category such as ALARM or CALL for increasing the chances of it appearing on the lock screen.

How do I run apps on lock screen?

Here's how to open your favorite apps directly from the Lock screen on your Galaxy device: Go to Settings > Lock screen > Shortcuts. Tap Left shortcut and pick your desired app. Do the same for Right shortcut.


Video Answer


1 Answers

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"
like image 138
Benjamin Avatar answered Oct 18 '22 21:10

Benjamin