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

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

问题描述

我有一项服务,该服务应与家庭自动化控制器(HAC)保持稳定的连接.在应用程序的首选项中,我将为用户提供一个选择,以使**保持连接到HAC或在不使用该应用程序时断开与HAC的连接.我打算使用startService()来查看首选项,以查看是否有必要从Service内调用stopSelf().

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.

我基本上有两个问题:

  1. 是否有必要从App中的每个Activity绑定到服务?(我发现在SplashScreen活动启动并绑定到服务后,似乎似乎没有执行onBind()).我什至注释掉了Activity中对bindService()的调用,但我仍然可以使用对Service的调用.我希望使用onBind()回调来验证服务是否仍然具有与HAC的有效连接.

  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.

如果我使用startService(),然后使用bindService();如果没有其他对象绑定,unBindService()将允许服务停止吗?另外,如果我使用bindService()启动服务,然后随后调用startService();.发生什么了?我试图了解这两种与服务交互的方式如何影响生命周期.

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.

谢谢,J

**我意识到这可能会损害电池寿命,但是当NotificationManager告诉您前门的运动检测器刚被绊倒时,这有时会很好.

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

推荐答案

我有一项服务,该服务应与家庭自动化控制器(HAC)保持稳定的连接.在应用程序的首选项中,我将为用户提供一个选择,以使**保持连接到HAC或在不使用该应用程序时断开与HAC的连接.我打算使用startService()来查看首选项,以查看是否有必要从Service内调用stopSelf().

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?

如果您正在使用 startService()来启动它,则不必从应用程序中的任何活动绑定到该服务.

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

我希望使用onBind()回调来验证服务是否仍然具有与HAC的有效连接.

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

特别是对于轻量级的,只读的,不太可能引起GC发出的问题(例如连接状态),我只是将其粘贴在静态数据成员中.现在,如果您需要比这更复杂的API,则需要绑定,但是绑定涉及到配置更改(例如屏幕旋转)在身体各个部位的痛苦.

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

如果我使用startService(),然后使用bindService();如果没有其他对象绑定,unBindService()会允许服务停止吗?

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

否,无论 unbindService()为何, startService()都会保留它.

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

此外,如果我使用bindService()启动服务,然后再调用startService();会发生什么?

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天全站免登陆