0

In a service I prepare some data then store this data in a static member variable, send an intent without data via Broadcast to the main UI activity so that the activity knows that the data has been prepared and is ready to access.

This works well, but since I need to do this about 10 times per second I thik that sending an impuls via Broadcast (containing no data at all) is a bit overkill and I have tested that a lot of time is wasted there.

Is there a better solution to do this?

DominicM
  • 2,186
  • 5
  • 24
  • 42

1 Answers1

0

You should bind your Service to your Activity and use either a simple callback or the Message and Handler facilities to send your data around. From what little you've said, a simple callback will probably suit your needs just fine.

Argyle
  • 3,324
  • 25
  • 44
  • The problem is that the service also needs to run when the activity is closed and from what I know this is not possible when it is bound to the activity, or is it? – DominicM May 24 '12 at 20:52
  • I have found a question concerning the same topic: http://stackoverflow.com/questions/2621395/more-efficient-way-of-updating-ui-from-service-than-intents I don't quite understand it though. Could someone explain it? – DominicM May 24 '12 at 21:09
  • @DominicM It's possible to unbind from a `Service` when your `Activity` stops and goes out of focus. The `Service` will continue running. This is how media players that run in the background work. – Argyle May 24 '12 at 21:17
  • But the lifecycle of a bound service shows that it gets destroyed when no client is bound anymore: http://developer.android.com/guide/topics/fundamentals/bound-services.html How to prevent this? – DominicM May 24 '12 at 21:35
  • @DominicM Make sure you use return [`START_STICKY`](http://developer.android.com/reference/android/app/Service.html#START_STICKY) when you start your `Service` so it keeps going when it's not processing a command. – Argyle May 24 '12 at 21:44