How To Properly Update A Widget In Android 8.0 – Oreo – API 26

You don’t indicate what the update trigger mechanism is. You seem concerned about latency (“Your widget may or may not get updated for a while”), so I am going to assume that your concern is tied to user interaction with the app widget, such as tapping a button.

Use JobScheduler to schedule a job as quickly as possible. Your widget may or may not get updated for a while.

This is a variation on “use JobIntentService“, which AFAIK is the recommended solution for this sort of scenario.

Other options include:

  • Use getForegroundService() with PendingIntent. With this, you effectively “pinky swear” that your service will call startForeground() within the ANR timeframe. If the work takes longer than a few seconds, call startForeground() to ensure that Android doesn’t get cranky. This should minimize the number of time the foreground notification appears. And, if the user tapped a button and you are still busy doing work a few seconds later, you probably want to show a notification or otherwise do something to let the user know that what they asked for is still in progress.

  • Use goAsync() on BroadcastReceiver, to do work in the context of the receiver while not tying up the main application thread. I haven’t tried this with Android 8.0+, so YMMV.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)