开发者

Does each Activity need to Bind to a Service & What happens when it was created with startService()

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:

  1. 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.

  2. 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 Servi开发者_如何转开发ce 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.


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.

This is a refreshingly rational and sensible approach -- let the user control how her device is being used. Kudos!

Is it necessary to Bind to the service from every Activity in the App?

It is not necessary to bind to the service from any activity in the app, if you are using startService() to start it.

I was hoping to use the onBind() callback to verify if the Service still has a valid connection to the HAC.

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).

If I use startService(), and then bindService(); will unBindService() then allow the service to stop if there are no other objects bound?

No, the startService() will keep it around, regardless of unbindService().

Also, if I start the service with bindService() and then subsequently call startService(); what happens?

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. :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜