greenoldman
greenoldman

Reputation: 21082

What can kill a service?

I wrote small app (service) for Android and it runs most of the time, it correctly restarts after booting the phone, however from time to time (phone is on at that time) it quits or gets killed.

Since it is passive service (it waits for screen lock on/off events) I wonder what in Android system can kill a process in such way, that process marked for rerun on boot is not activated again.

Or in other words, how to detect/prevent such case that a service intended to run all the time gets killed and not activated again?

Just for the record I have an option to uninstall the service ;-).


Update: my question is general one, but I have this service in mind: https://github.com/macias/MissingSettings

As for onStart vs. onCommand question:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent,flags,startId);
    // If we get killed, after returning from here, restart
    return START_STICKY;
}

Upvotes: 0

Views: 179

Answers (1)

Volodymyr Lykhonis
Volodymyr Lykhonis

Reputation: 2976

Basically nothing can prevent to be killed any time. Low memory is good example. In your case I believe Android system thinks there is no UI so the service is not really required. It makes sense to free resources for the apps which are on foreground.

If your service really required and then it should bring up to user using startForeground with notification.

Also you can try relay on onStartCommand return result, it might can help you to get intent back from system.

p.s. it would be much easier for us to give advice how to design service only if you'd share your requirements

Upvotes: 1

Related Questions