服务生命周期的开始和约束? [英] lifecycle of Service that is started and bound?

查看:135
本文介绍了服务生命周期的开始和约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感到困惑的服务生命周期。结果
我有以下诸多问题:


  1. 如果我的应用程序调用 startService()多次到服务,是否有同时运行多个服务?或只有一个?


  2. 作为1,如果我叫 bindService()多次在我的应用程序服务,会发生什么事我的应用程序?


  3. 假设我有一个已经通过启动的服务startService(),再后来我绑定,以指示它做一些事情。在这种情况下,如果我指示(通过此Binder接口)来执行它的 stopSelf()方法的服务,并正在运行的服务立即停止?


  4. 再假设我有一个只能通过启动服务bindService() onUnbind 被覆盖,返回true,在这种情况下,我应该叫 stopSelf 明确的方法来关闭服务?



解决方案

是的,你可以同时运行多个服务。但是,如果你正在使用的服务,你必须小心这样做。如果你开始一个服务,你必须停止像的onDestroy任何方法()或自定义方法的服务,因为每当你完成你的活动,该服务仍处于运行背景。

一件事:使用服务,只有当你需要像媒体播放器后台进程;否则,就可以实现所有这些与活动。

有关详细信息,请阅读 Android开发者服务文档。

I am confused about Service lifecycle.
I have many questions following:

  1. If my app calls startService() multiple times to a Service, will there be multiple Services running simultaneously? Or only one?

  2. As 1st, if I call bindService() multiple times to a Service in my app, what will happen to my app?

  3. 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 its stopSelf() method, does the running Service stop immediately?

  4. Assume again that I have a Service that can be started only by bindService(), and the onUnbind is overridden to return true, in this case, should I call stopSelf 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屋!

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