The documentation for Services says:

Binding to a Started Service 
>
> As discussed in the 
> Services<http://developer.android.com/guide/components/services.html>document,
>  you can create a service that is both started and bound. That is, 
> the service can be started by calling 
> startService()<http://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29>,
>  
> which allows the service to run indefinitely, and also allow a client to 
> bind to the service by calling 
> bindService()<http://developer.android.com/reference/android/content/Context.html#bindService%28android.content.Intent,%20android.content.ServiceConnection,%20int%29>.
>  
>
>
> If you do allow your service to be started and bound, then when the 
> service has been started, the system does *not* destroy the service when 
> all clients unbind. Instead, you must explicitly stop the service, by 
> calling 
> stopSelf()<http://developer.android.com/reference/android/app/Service.html#stopSelf%28%29>or
>  
> stopService()<http://developer.android.com/reference/android/content/Context.html#stopService%28android.content.Intent%29>
> .
>
This is the kind of Local Service I want to make as part of my app.  The 
service should run continuously for as long as my app is instantiated.  
That is because it manages a Bluetooth connection to a serial port that 
needs constant monitoring, even when my main activity is suspended, so that 
when my main activity is brought to the foreground, the results of that 
continuous monitoring of the serial port are available.  I can't be closing 
and re-opening the Bluetooth connection every time my activity is paused.  
So I plan to call startService and bindService from my main activity.  
startService will be called from onCreate in my activity.  It is OK to call 
startService several times if my activity is created several times.  Then I 
will call bindService from my onResume and unbindService from my onPause.  
I only need to communicate with the service when my activity is foreground, 
even though I need the service to continue to run the rest of the time too.

My problem is that documentation above which says I must call stopService() 
to destroy the service when my app is done with it.  But where can I do 
that?  I can't do it from my activity's onDestroy because that could be 
called multiple times by the OS in the course of my app's lifetime.  There 
is no reliable place where I can be sure my whole app is being killed.  
Android does not provide any such message.  What if I just let the OS kill 
my service whenever it feels like it?  Will that be OK?  When the OS 
decides to kill my app, does it call the service's onDestroy() method, even 
if nothing in my app ever calls stopService()?  In my onDestroy() method I 
plan to do an orderly shutdown of the Bluetooth connection - close the 
streams, close the socket, stop the threads, etc.  Will I get a chance to 
do this in my service if my app call startService and never explicitly 
calls stopService?

-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to