Pergunta

I have a service running in its own process. It appears fine in the evening, but after I've gone to sleep I think Android takes its axe to it.

Am I right in thinking that onDestroy() won't be called when Android kills a service? If not, is there any other place the kill is registered?

I think I'm going to need to research the AlarmManager.

Foi útil?

Solução

I have a service running in its own process.

Why is it in its own process?

Am I right in thinking that onDestroy() won't be called when Android kills a service?

It may, it may not.

If not, is there any other place the kill is registered?

No.

I think I'm going to need to research the AlarmManager.

Users hate services that live forever, which is why task killers are popular. They hate services that run in their own process even more, because they consume extra RAM, CPU, and battery, typically for no good reason.

If your objective is to do something on a periodic basis, please use AlarmManager -- that's why it is there.

Outras dicas

Accordint to the Service lifecycle in the developer document, the onDestroy() method will be called when the service is killed or stops.

Also, if an app has a service running in the background(i.e a music player playing in the background), the system will consider that app is active and won't kill its process except extreme conditions( I don't think it happens in practice).

The document says:

Note this means that most of the time your service is running, it may be killed by the system if it is under heavy memory pressure. If this happens, the system will later try to restart the service. An important consequence of this is that if you implement onStartCommand() to schedule work to be done asynchronously or in another thread, then you may want to use START_FLAG_REDELIVERY to have the system re-deliver an Intent for you so that it does not get lost if your service is killed while processing it. 

Here, what I am thinking about is that if a service is killed by the os, the os will try to restart it later. But, in this case, is the onDestroy() method called?I'm not sure. Have someone tested on this?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top