6

I need a service to be running in the background for my app and I want it start up automatically when the phone is switched on. I've got the usual intent-filter for BOOT_COMPLETED but what happens is this...

  1. I switch the phone on.
  2. It starts up until the lock screen.
  3. I go through the lock screen.
  4. The background job gets started up.

I want/need the job to start before I'm asked for the lock screen.

This is on a Pixel running Android N.

Cheers.

Mark Sheekey
  • 608
  • 7
  • 23
  • Related questions: - [Start Activity screen even if screen is locked in Android](https://stackoverflow.com/q/20113161/1455694) - [How to start an Android App From Lockscreen?](https://stackoverflow.com/q/41830431/1455694) – Anton Samokat Oct 27 '22 at 15:45

1 Answers1

12

Your problem is due to Android 7.0 running a secure Direct Boot mode when the device has been powered on but the user has not unlocked the device. This limits Credential encrypted storage from being accessed before the user has unlocked the device. Therefore your app has to register to use Device encrypted storage for use during Direct Boot mode and after the user has unlocked the device.

This article discusses how it works and example use cases (scheduled notifications, SMS apps, etc) but what your code is missing is a receiver and intent-filter for ACTION_LOCKED_BOOT_COMPLETED.

Essentially, you need the following code in your manifest

<receiver
  android:directBootAware="true" >
  ...
  <intent-filter>
    <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
  </intent-filter>
</receiver>

Let me know if you need any more help!

Joe B.
  • 471
  • 3
  • 7