android 服务 startService() 和 bindService()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3514287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android service startService() and bindService()
提问by maxsap
I would like to know if it is possible to have a service that is started with startService and then be able to also bind to that service and do some remote procedure calls? according to this:http://developer.android.com/guide/topics/fundamentals.html#servlife
我想知道是否有可能有一个以 startService 启动的服务,然后也可以绑定到该服务并执行一些远程过程调用?根据这个:http: //developer.android.com/guide/topics/fundamentals.html#servlife
the two services have different lifecycle so it's not possible,does anyone know about it?
这两个服务有不同的生命周期,所以这是不可能的,有人知道吗?
回答by Falmarri
I think hara's answer was a little confusing. What you describe is perfectly legitimate and in fact the only way to get the behavior you want. If you create a Service
by binding to it, it will die when you unbind. So the only way to keep it around without activities binding to it is to start it with startService()
. There is no conflict with lifecycles because it only applies to how the service is STARTED. So once it's started with startService()
, it follows that lifecycle process. So you are free to bind and unbind to it as much as you wish and it will only die when you call stopService()
or stopSelf()
.
我认为 hara 的回答有点令人困惑。您所描述的是完全合法的,实际上是获得您想要的行为的唯一方法。如果你Service
通过绑定来创建一个,它会在你解除绑定时死亡。因此,在没有活动绑定到它的情况下保留它的唯一方法是以startService()
. 与生命周期没有冲突,因为它只适用于服务的启动方式。所以一旦它以 开始startService()
,它就会遵循那个生命周期过程。因此,您可以随心所欲地绑定和解除绑定,并且只有在您调用stopService()
或时它才会死亡stopSelf()
。
回答by Yuliia Ashomok
Yes, You can start and bind (one or more times) the same service.
The following flowchart demonstrates how the lifecycle of a service is managed. The variable counter tracks the number of bound clients:
是的,您可以启动和绑定(一次或多次)相同的服务。
以下流程图演示了如何管理服务的生命周期。变量 counter 跟踪绑定客户端的数量:
Good example - music app. Explanation from Building a Media Browser Serviceofficial tutorial:
很好的例子 - 音乐应用程序。来自构建媒体浏览器服务官方教程的说明:
A service that is only bound (and not started) is destroyed when all of its clients unbind. If your UI activity disconnects at this point, the service is destroyed. This isn't a problem if you haven't played any music yet. However, when playback starts, the user probably expects to continue listening even after switching apps. You don't want to destroy the player when you unbind the UI to work with another app.
For this reason, you need to be sure that the service is started when it begins to play by calling startService(). A started service must be explicitly stopped, whether or not it's bound. This ensures that your player continues to perform even if the controlling UI activity unbinds.
To stop a started service, call Context.stopService() or stopSelf(). The system stops and destroys the service as soon as possible. However, if one or more clients are still bound to the service, the call to stop the service is delayed until all its clients unbind.
当所有客户端解除绑定时,仅绑定(未启动)的服务将被销毁。如果此时您的 UI 活动断开连接,则服务将被销毁。如果您还没有播放任何音乐,这不是问题。然而,当播放开始时,用户可能希望即使在切换应用程序后继续收听。当您解除 UI 绑定以使用另一个应用程序时,您不想破坏播放器。
为此,您需要通过调用 startService() 来确保服务在开始播放时已启动。必须明确停止已启动的服务,无论它是否绑定。这可确保即使控制 UI 活动解除绑定,您的播放器也能继续执行。
要停止启动的服务,请调用 Context.stopService() 或 stopSelf()。系统会尽快停止并销毁服务。但是,如果一个或多个客户端仍绑定到该服务,则停止服务的调用会延迟,直到其所有客户端解除绑定。
From Serviceref:
从服务参考:
A service can be both started and have connections bound to it. In such a case, the system will keep the service running as long as either it is started or there are one or more connections to it with the Context.BIND_AUTO_CREATE flag. Once neither of these situations hold, the service's onDestroy() method is called and the service is effectively terminated.
一个服务既可以启动也可以绑定连接。在这种情况下,只要服务已启动,或者存在一个或多个带有 Context.BIND_AUTO_CREATE 标志的连接,系统就会保持服务运行。一旦这两种情况都不成立,服务的 onDestroy() 方法将被调用,服务将被有效终止。
回答by hara
If you start a service using startService(), then you should stop it using stopService().
如果您使用 startService() 启动服务,那么您应该使用stopService()停止它。
There are two reasons that a service can be run by the system. If someone calls Context.startService() then the system will retrieve the service (creating it and calling its onCreate() method if needed) and then call its onStartCommand(Intent, int, int) method with the arguments supplied by the client. The service will at this point continue running until Context.stopService() or stopSelf() is called. Note that multiple calls to Context.startService() do not nest (though they do result in multiple corresponding calls to onStartCommand()), so no matter how many times it is started a service will be stopped once Context.stopService() or stopSelf() is called; however, services can use their stopSelf(int) method to ensure the service is not stopped until started intents have been processed.
系统可以运行服务有两个原因。如果有人调用 Context.startService() ,则系统将检索服务(创建它并在需要时调用其 onCreate() 方法),然后使用客户端提供的参数调用其 onStartCommand(Intent, int, int) 方法。此时服务将继续运行,直到 Context.stopService() 或 stopSelf() 被调用。请注意,对 Context.startService() 的多次调用不会嵌套(尽管它们确实会导致对 onStartCommand() 的多次相应调用),因此无论服务启动多少次,一旦 Context.stopService() 或 stopSelf () 叫做; 但是,服务可以使用它们的 stopSelf(int) 方法来确保在处理启动的意图之前服务不会停止。
You could bind to the service as many ServiceConnection as you want with bindService(), but pay attention to the flag you passed to it. If you pass 0 then if you call stopService() the service will stopped(i don't know exacltly what happens to you ServiceConnection). Otherwise if you want your service alive untill the ServiceConnection is binded to it then use BIND_AUTO_CREATE.
您可以使用bindService() 将任意数量的ServiceConnection 绑定到服务,但请注意传递给它的标志。如果你传递 0 那么如果你调用 stopService() 服务将停止(我不知道你的 ServiceConnection 会发生什么)。否则,如果您希望您的服务一直存在,直到 ServiceConnection 绑定到它,然后使用BIND_AUTO_CREATE。
this is from stopService():
这是来自 stopService():
Request that a given application service be stopped. If the service is not running, nothing happens. Otherwise it is stopped. Note that calls to startService() are not counted -- this stops the service no matter how many times it was started.
Note that if a stopped service still has ServiceConnection objects bound to it with the BIND_AUTO_CREATE set, it will not be destroyed until all of these bindings are removed. See the Service documentation for more details on a service's lifecycle.
This function will throw SecurityException if you do not have permission to stop the given service.
请求停止给定的应用程序服务。如果服务未运行,则什么也不会发生。否则停止。请注意,对 startService() 的调用不计算在内——无论服务启动多少次,这都会停止服务。
请注意,如果已停止的服务仍然使用 BIND_AUTO_CREATE 集绑定了 ServiceConnection 对象,则在删除所有这些绑定之前,它不会被销毁。有关服务生命周期的更多详细信息,请参阅服务文档。
如果您无权停止给定的服务,此函数将抛出 SecurityException。
i hope this helps..
我希望这有帮助..