[android-developers] Re: "Proper" way to communicate service errors to activity
you can use ResultReceiver, create one in your activity and pass it to the service then send messages from the Service to the Activity with receiver.send()
On Friday, September 21, 2012 5:57:35 PM UTC+3, Cool Frood wrote:
Hi,--I'm trying to implement a service that is started by an activity. When the service starts off, it spawns a worker thread that makes a socket connection. If the socket connection fails, I want to stop the service. All this is working fine, but I don't have a good handle of how to indicate to the activity that the service could not start successfully, and has, therefore, turned itself off.One way to do this is to communicate via a listener that is attached into the service in the onServiceConnected() connection callback. The problem with this approach is that, depending on when the worker thread notices the error, the onServiceConnected() may or may not have been called, and therefore the listener may or may not have been attached by the time the worker thread decides to shut down the service.I was hoping that onServiceDisconnected() would be called, but that is only called in case of an unexpected crash, and isn't really useful when the service and activity exist in the same process.My question is, what is the best way to approach something like this? It seems silly to me that even though the activity and service exist in the same process, the activity cannot figure out the state of the service that easily. I _can_ use a static state variable within the service, but that still doesn't allow the activity to be notified of the event.Thanks!
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home