I have a Service that is supposed to maintain a constant connection with a home automation controller (HAC). In the preferences of the App, I will give the User a choice to stay** connected to the HAC or to drop the connection to the HAC when the App is not in use. I intend to use startService(), with a look at the preferences to see if it's necessary to call stopSelf() from within the Service.
I basically have two questions:
Is it necessary to Bind to the service from every Activity in the App? (I'm finding that the onBind() doesn't seem to execute after my SplashScreen Activity has started and binded to the Service). I even commented out the call to bindService() from an Activity and I'm still able to use the calls the Service. I was hoping to use the onBind() callback to verify if the Service still has a valid connection to the HAC.
If I use startService(), and then bindService(); will unBindService() then allow the service to stop if there are no other objects bound? Also, if I start the service with bindService() and then subsequently call startService(); what happens? I'm trying to understand how these two approaches to interacting with the Service affect the lifecycle.
Thanks, J
** I realize this can be detrimental to battery life, but sometimes it's nice when the NotificationManager tells you that the Motion Detector on your front porch has just been tripped.
This is a refreshingly rational and sensible approach -- let the user control how her device is being used. Kudos!
It is not necessary to bind to the service from any activity in the app, if you are using
startService()
to start it.Particularly for lightweight, read-only, not-likely-to-cause-GC-issues stuff like a connection status, I'd just stick it in a static data member. Now, if you need a more complex API than that, then you'll need binding, but binding is a pain in various body parts when it comes to configuration changes (e.g., screen rotations).
No, the
startService()
will keep it around, regardless ofunbindService()
.Ummm...can you be more specific? I mean, I feel reasonably comfortable in saying that your sequence of events will not cause a rupture in the space-time continuum, nor will it cause me to spontaneously grow hair. :-)