How can start Service several times while Service

2019-08-30 02:12发布

问题:

I want to do this acts with my Service :

1)start App

3)start a Service by App

4)update my UI by Service

5)close App but the Service keep work

6)sart App again

7)connect to Service (not run new Service)

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

回答1:

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.

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.