Search code examples
javaandroid-things

Android Things - UpdatePolicy not applied


I'm programming an update interface in my Android Things project. I can do manual update, with an user input. But I'm trying to schedule an auto-update every night at midnight. I want to use a custom UpdatePolicy with a deadline but I failed to use it.

I tried this in the onCreate method in my activity :

mUpdateManager.setPolicy(
     new UpdatePolicy.Builder()
         .setPolicy(POLICY_APPLY_AND_REBOOT)
         .setUpdateDeadline(10, TimeUnit.SECONDS)
         .build());

But there isn't any update after 10 seconds. Maybe, I don't understand the deadline. Do I use it wrong ?


Solution

  • The deadline has nothing to do with when an update check is performed. The usual schedule of update checks is

    • once shortly after boot
    • once every 5 hours (approximately) thereafter

    (These times are not exact for reasons that aren't relevant to this discussion.)

    The deadline reflects how long the device will let an available update sit without being applied before the device will force it to apply and reboot. The device doesn't know about an available update until it performs a check, so you could be waiting up to 5 hours for that.

    The deadline is meant to operate on a longer timescale (for instance, 5 days, a week, etc). This is useful as a fallback in case there's some kind of bug with the update scheduler, or in case you allow users to postpone the update but don't want them to be able to do that forever.

    To achieve what you want, you should schedule (using WorkManager, JobScheduler, etc) a task that runs at midnight each day and calls UpdateManager.performUpdateNow(UpdatePolicy.POLICY_APPLY_AND_REBOOT)

    TL,DR: Update checks are very much a background thing. If you care about timing at all, use UpdateManager.performUpdateNow, but no more than once every 5 hours.