是否每个活动需要绑定到一个服务与;当它被用startService创建会发生什么情况() [英] Does each Activity need to Bind to a Service & What happens when it was created with startService()

查看:127
本文介绍了是否每个活动需要绑定到一个服务与;当它被用startService创建会发生什么情况()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务,应该保持与家庭自动化控制器(HAC)的持续连接。在应用程序的preferences,我都会给用户一个选择留下**连接到HAC或断开连接到HAC时,应用程序不使用。我打算用startService(),一起来看一下在preferences,看是否有必要在服务中调用stopSelf()。

我基本上有两个问题:

  1. 是否需要从应用程序的每个活动绑定到服务? (我发现了onBind()似乎并没有执行后,我闪屏活动已经开始,并绑定到该服务)。我甚至注释掉从活动调用bindService(),我仍然能够使用电话服务。我希望用onBind()回调来验证,如果服务仍然有到HAC一个有效的连接。

  2. 如果我用startService(),然后bindService();将unBindService(),则允许该服务停止,如果有必然没有其他物体?另外,如果我开始bindService(服务),并随后调用startService();怎么了?我想了解这两种方法与服务互动如何影响生命周期。

谢谢, Ĵ

**我知道这可能会损害电池寿命,但有时它是很好的时候NotificationManager告诉你,运动检测器上的前廊刚刚被绊倒。

解决方案
  

我有一个服务,应该保持与家庭自动化控制器(HAC)的持续连接。在应用程序的preferences,我都会给用户一个选择留下**连接到HAC或断开连接到HAC时,应用程序不使用。我打算用startService(),一起来看一下在preferences,看是否有必要在服务中调用stopSelf()。

这是一个令人耳目一新的理性和明智的做法 - 让用户控制如何她的设备的使用情况。奖励!

  

是否需要从应用程序?

每一个活动绑定到服务

这是没有必要绑定到该服务的任何活动的应用程序,如果您使用的是 startService()来启动它。

  

我希望使用onBind()回调来验证,如果服务仍然有到HAC一个有效的连接。

特别是对于轻量级的,只读的,不,可能对事业-GC-问题的东西,如连接状态,我只是坚持在一个静态数据成员。现在,如果你需要一个更复杂的API比,那么你就需要结合,但结合是身体各部位疼痛,当涉及到的配置变化(如屏幕旋转)。

  

如果我使用startService(),然后bindService();将unBindService(),则允许该服务停止,如果有必然没有其他物体?

没有,在 startService()将保持它周围,无论的unbindService()

  

另外,如果我开始bindService(服务),并随后调用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 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.

解决方案

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

这篇关于是否每个活动需要绑定到一个服务与;当它被用startService创建会发生什么情况()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆