1

I need to download some files (10-20 or depends on user) from service in my app. The problem is I am using IntentSevice and finding it hard to update activity UI. I know following ways to update UI

  1. Use handler and send messages from service to activity using Messenger like this.
  2. Send broadcast intents.

Using first method would cause problem once activity is closed & re-opened and also I am not sure about its performance. Using second would definitely cause perfomance issues since I need to update UI quite frequently (once or twice every two seconds). Is there anyother possible way of exchanging data between IntentService and Activity which is efficient? or I have to switch it to Bound Service?

Community
  • 1
  • 1
ashu
  • 1,756
  • 4
  • 22
  • 41

2 Answers2

3

Is there anyother possible way of exchanging data between IntentService and Activity which is efficient?

Use an event bus, like LocalBroadcastManager, greenrobot's EventBus, or Square's Otto. Have the IntentService post events as needed (e.g., when a file is done downloading). Your activities/fragments can register and unregister for events as they come and go from the foreground. If they are in the foreground, they will receive the event and be able to update the UI. You can even detect if the event was not picked up by the foreground UI and have the service display a Notification, if desired.

This directory contains three sample apps demonstrating this for the three event bus implementations that I cited. And, FWIW, here is the PDF of slides that I used in recent webinars on using event buses.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • just to add an example to your answer: http://stackoverflow.com/questions/8802157/how-to-use-localbroadcastmanager – user1779374 Jun 12 '14 at 11:54
  • @PankajKumar: "Can we use IntentSender in this scenario?" -- I don't see how it would solve any of the OP's concerns. – CommonsWare Jun 12 '14 at 11:55
  • So while using LocalBroadcastManagers, if I stick to my orginal plan of updating UI once every two seconds, Will it be okay then? – ashu Jun 12 '14 at 12:01
  • @ashu: I don't see any problem with that. An in-process event bus itself has little overhead. – CommonsWare Jun 12 '14 at 12:43
  • @CommonsWare : I know its a bit late, thanks to one of my friends who insisted me to join him on something else, but consider this scenario : An event is sent from service to activity, lets say, every 10 seconds with some progress info. The activity doesn't have anything else but displays the progress only and user can close it and re-open it from `Notification`. Lets say when the event was sent, the activity was __not__ in foreground and 2 secs later, user opens the activity. Well in this case, wouldn't activity be displaying nothing until next event is received? – ashu Jan 14 '15 at 07:02
  • @ashu: There are any number of solutions for that (having the activity get the current progress in `onResume()`, using a sticky event with greenrobot's EventBus, using an event producer with Otto, etc.). – CommonsWare Jan 14 '15 at 12:13
  • Oh! I didn't knew about sticky events! BTW, I have done something different but I am not too sure if its 100% fool proof. Yesterday, I also wrote a [blog post](http://ashutoshgangwar.blogspot.com/2015/01/binding-intentservice-to-activity.html) about what I did. But I am having a moral crisis that whether I should stick to `by-the-book` methods or not. – ashu Jan 14 '15 at 13:51
0

Try LocalBroadcastManager (http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html). Faster than Broadcast Service.

brummfondel
  • 1,202
  • 1
  • 8
  • 11