file upload - Android IntentService/Service to request/send data to web service and getting the progress to UI -
on android basically, trying upload/download data web service , have progress bar should rendered showing progress percentage.
following approach implemented along reason considering them:
- chose
intentservice
run in background: per link not use bound service destroy when binded activity/fragment destroyed. hence selection ofintentservice
onservice
. move on 1 time , intent service works on worker thread better. - broadcast receive updates: implementation of
localbroadcastmanager
update progress bar ui registering listen broadcast receivedintentservice
described above.
however approach, following issue needs addressed:
sequence of broadcasts not maintained. broadcast carries percentage of progress of upload/download. since sequence not maintained, there chances receive stale updates (eg: may receive 30% after 40%). tried using sendbroadcastsync
method of localbroadcastmanager
doesn't work consistently (i not sure on this). hence tried implement messenger
via handler
got know approach not able reconnect on going upload/download once ui re-created (activitiy/fragment destroyed , created). reference handler
lost along previous activity/fragment , hence messages not delivered ui update.
i have been trying way since while not able done. words of wisdom helpful.
i go problem bit differently, ordering of messages may problem variety of message solutions.
the fact 40% event after 30% event not problem. is problem if update ui reflect 30%.
have each event (each intent
when using localbroadcastmanager
event bus) contain timestamp along percentage-complete. track in ui last-seen event timestamp. if event older last-seen event timestamp, ignore event.
if sure percentage monotonically increasing — in other words, there no legitimate scenario in might drop 40% 30% complete — skip timestamp , apply new percentages higher currently-showing percentage.
Comments
Post a Comment