服务处于活动状态时,如何可以多次启动服务 [英] How can start Service several times while Service is alive

查看:79
本文介绍了服务处于活动状态时,如何可以多次启动服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过我的服务执行此操作:

I want to do this acts with my Service :

1)启动应用

3)通过应用启动服务

3)start a Service by App

4)通过服务更新我的用户界面

4)update my UI by Service

5)关闭应用程序,但服务可以继续工作

5)close App but the Service keep work

6)再次销售App

6)sart App again

7)连接到服务(不运行新服务)

7)connect to Service (not run new Service)

我希望在服务期间不更改服务中的公共变量.

I want in my service public variable during living Service do not change.

推荐答案

如果您从onStartCommand()返回START_STICKY,那么您的Service将一直运行直到明确停止.然后,可以通过调用stopService()或通过Service调用stopSelf()(当它不再运行时)来决定要停止Service的方式.

If you return START_STICKY from onStartCommand() then your Service will run unstil it is explicitly stopped. You can then decide how you want to stop your Service, either by something calling stopService() or by the Service calling stopSelf() when it no longer wants to run.

请注意,Android随时可能会杀死您的Service.如果您返回了START_STICKY,则Android将在杀死它后重新启动您的Service,但是您无法采取任何措施来确保Android不会杀死您的Service.

Please also note that Android can kill your Service pretty much whenever it wants to. If you have returned START_STICKY then Android will restart your Service after it kills it, but there is nothing you can do to ensure that Android will not kill your Service.

这篇关于服务处于活动状态时,如何可以多次启动服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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