服务生命周期的开始和约束? [英] lifecycle of Service that is started and bound?
问题描述
我感到困惑的服务生命周期。结果
我有以下诸多问题:
-
如果我的应用程序调用
startService()
多次到服务,是否有同时运行多个服务?或只有一个? -
作为1,如果我叫
bindService()
多次在我的应用程序服务,会发生什么事我的应用程序? -
假设我有一个已经通过
启动的服务startService()
,再后来我绑定,以指示它做一些事情。在这种情况下,如果我指示(通过此Binder接口)来执行它的stopSelf()
方法的服务,并正在运行的服务立即停止? -
再假设我有一个只能通过
启动服务bindService()
和onUnbind
被覆盖,返回true,在这种情况下,我应该叫stopSelf
明确的方法来关闭服务?
是的,你可以同时运行多个服务。但是,如果你正在使用的服务,你必须小心这样做。如果你开始一个服务,你必须停止像的onDestroy任何方法()
或自定义方法的服务,因为每当你完成你的活动,该服务仍处于运行背景。
一件事:使用服务,只有当你需要像媒体播放器后台进程;否则,就可以实现所有这些与活动。
有关详细信息,请阅读 Android开发者服务文档。
I am confused about Service lifecycle.
I have many questions following:
If my app calls
startService()
multiple times to a Service, will there be multiple Services running simultaneously? Or only one?As 1st, if I call
bindService()
multiple times to a Service in my app, what will happen to my app?Assume that I have a Service that has been started via
startService()
, and then later I bind it in order to instruct it to do something. in this case, if I instruct (via this Binder interface) the Service to execute itsstopSelf()
method, does the running Service stop immediately?Assume again that I have a Service that can be started only by
bindService()
, and theonUnbind
is overridden to return true, in this case, should I callstopSelf
method explicitly to shutdown the Service?
Yes, you can run multiple services simultaneously. But if you are using a service, you have to do so with care. If you are starting a service, you have to stop that service on any method like onDestroy()
or custom method, because whenever you finish your activity, the service is still running in background.
One more thing: use service only when you need background process like media player; otherwise, you can achieve all these with activity.
For more information read the Android developer service document.
这篇关于服务生命周期的开始和约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!