You can always maintain an error queue, so if you notice some errors
before the activity is attached, you can propagate them back to the
Activity.  I've done this before, and had success doing so.  (In fact,
we do this a fair amount in the security libraries we write for our
projects here, because of the nature of when services are bound and
when they are needed, etc...)

kris

On Fri, Sep 21, 2012 at 10:57 AM, Cool Frood <[email protected]> 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 [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to