How to wait for service on activity start
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 w开发者_如何转开发ant 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!
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.
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?
精彩评论