1

I am making an application that has Activity which communicates with a single service, and is used to start, stop or change settings of that service. I used a singleton approach from this tip.

My problem is that busy-waiting is not working for me and I can't attach a listener to the service because activity gets blocked. I want the service to start or get it's current instance at application start so I put the busy-waiting in onCreate. I'm guessing I'm doing this very wrong, so how do I do this appropriately?

Also if there is a better approach to this, please post any links or guides, cuz I'm getn confused on android developer site :P

thanks!

Community
  • 1
  • 1
danizmax
  • 2,446
  • 2
  • 32
  • 41

2 Answers2

2

I used a singleton approach from this tip.

Don't do that. Use bindService() and the local binding pattern instead, and you will be notified when the service is ready. You also get access to an API published by the service and can start using it once you are bound.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • bindService sounds good, but as far as I know service stays alive only until Activity gets closed. I need the service to stay alive and reconnect it again on next activity start. – danizmax Nov 02 '10 at 13:20
  • @danizmax: You can use both `startService()` and `bindService()` if needed. Make sure you have a good reason for keeping the service going after the activity ends, though -- that sort of behavior, if a user is not expecting it, results in one-star ratings on the Market. You might also try operating your service with more of the command pattern, decoupling your activity more from the service operation. – CommonsWare Nov 02 '10 at 16:55
  • The service is the main feature, it will fetch data from the internet and process it every few minutes, depends from the settings. – danizmax Nov 02 '10 at 20:02
  • @danizmax: The service is rarely the main feature in the eyes of the user. – CommonsWare Nov 02 '10 at 20:31
  • Thanks for the tips... I've decided to use ongoing notifications so the user is aware of the running service and what it is doing... – danizmax Nov 03 '10 at 11:03
  • them main reason I don't want to use bindService() is because I don't want my app to show up if user long presses the home button after the app was closed, but still be visible in notification area. Does that make sense? – danizmax Nov 03 '10 at 11:10
1

The problem is one of procedural vs. event-driven programming, android's UI being built on the later.

onCreate is an event. Notification of a service connection is also an event. You won't be delivered the service connection notification (even though it's happened) until you return from onCreate.

Can you split your activity startup into two parts, the first done in onCreate and the second in response to the service connection?

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
  • That was actually my plan A, but how do I get notification from service when I don't have it's instance? Via BroadcastReceiver? I have a feeling it was not meant to be used for this purpose. – danizmax Nov 02 '10 at 13:17
  • From the onServiceConnected() method of your service connection, which is where you get the instance handle you'll later use to invoke the service's RPC's. – Chris Stratton Nov 02 '10 at 13:32