Search code examples
androidboot

Android BOOT_COMPLETED doesn't fire until user has gone past lock screen


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.


Solution

  • 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!