multithreading - How To Properly Update A Widget In Android 8.0 - Oreo - API 26 -
let's have widget app has targetsdkversion set 26. widget takes between 100ms , 10s update. of time under 1s. before android o, if onupdate() called on appwidgetprovider, launch background service update widget. however, android o returns illegalstateexception if attempt behavior. obvious solution of starting foreground service seems extreme measure done in under 10s 99% of time.
possible solutions
- start foreground service update widget. annoy user notification gone in 10s.
- use jobscheduler schedule job possible. widget may or may not updated while.
- attempt work in broadcast receiver. lock ui thread other apps. yuck.
- attempt work in widget receiver. lock ui thread other apps. yuck.
- abuse gcm background service running. lot of work , feels hacky.
i don't of above solutions. i'm missing something.
(even more frustrating app loaded memory system calling onupdate(). don't see how loading app memory call onupdate(), not giving app 1s update widget off ui thread saving battery life.)
you don't indicate update trigger mechanism is. seem concerned latency ("your widget may or may not updated while"), going assume concern tied user interaction app widget, such tapping button.
use jobscheduler schedule job possible. widget may or may not updated while.
this variation on "use jobintentservice", afaik recommended solution sort of scenario.
other options include:
use
getforegroundservice()pendingintent. this, "pinky swear" service callstartforeground()within anr timeframe. if work takes longer few seconds, callstartforeground()ensure android doesn't cranky. should minimize number of time foreground notification appears. and, if user tapped button , still busy doing work few seconds later, want show notification or otherwise let user know asked still in progress.use
goasync()onbroadcastreceiver, work in context of receiver while not tying main application thread. haven't tried android 8.0+, ymmv.
Comments
Post a Comment