1

I have database cleanup that I need to do within a running service thread. Under normal conditions, this will occur when the service is done and onDestroy() gets called.

I've noticed that if I reinstall or update the application while the service is running that onDestroy() is not called for the service. Is there anyway my application can know at the next startup that it's the first run after a reinstall so it can complete some cleanup on the database?

To clarify, simply running the service again won't do what I need. My service in many cases is started by a broadcastreceiver and not by the activity itself so I can't rely on the onPause for the main activity. I can't figure out how to know if the service might already be running or was silently killed and needs cleanup.

I'd like to clarify since others seem to have misunderstood. First, onDestroy just plain doesn't happen in the situation of a reinstall or update of the app. Neither do the shared preferences get reset because the app was not uninstalled. Second, Google specifically states you can't rely on the onDestroy, so berating me for finding a quick fix instead of asking Google why onDestroy isn't working doesn't quite make sense either. See the API docs about onDestroy for Activities. The service documentation doesn't repeat that warning but obviously it holds true. The only working solution I've found to date is the one I posted below from another StackOverflow thread.

Tony Maro
  • 1,854
  • 17
  • 14
  • I find it very confusing if your service is not killed properly, and onDestroy() isn't called, once you reinstall your application. Maybe it was best to ask Google for some insight on the topic instead of finding a quick fix around the problem? – harism Dec 28 '11 at 21:36
  • @Harism, Google does not guarantee that an onDestroy gets called ever. In fact, in the API docs they state "There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away." Specifically though if you reinstall / update an app whatever is running gets killed and onDestroy does not get called. The only event that is guaranteed to be called is onPause according to the documentation, and services don't have that event. – Tony Maro Dec 30 '11 at 23:39
  • True that. But personally I find it rather vague excuse not to call Service.onDestroy() similarly as Activity.onPause() gets called just because there's no Service.onPause() method. Quite the contrary actually, Service.onDestroy() clearly states to do proper cleanup before returning from it. It's a totally different story if Google doesn't care about possibly lost resources at that point and relies on killing the process only though. – harism Dec 31 '11 at 00:28
  • How about just tracking the "last known version" and if the version increments you know you were updated? – Jeffrey Blattman Sep 12 '18 at 21:23

1 Answers1

0

Aha, I found an answer here: How to check if a service is running on Android?

With this little snippet. Just pass a current context to it, so if coming from a BroadcastReceiver pass the context given the onReceive for example... With this I can set a global variable when the service starts, and clear it in the onDestroy. When my app starts if the variable is set I can test to see if the service is currently running and if not then I can do a cleanup.

private boolean isMyServiceRunning(Context context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if ("com.myapp.myservice".equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}
Community
  • 1
  • 1
Tony Maro
  • 1,854
  • 17
  • 14
  • Hi tony... Can u update us with what conclusion you have drawn for the service. Does it gets destroyed and restarts itself or it does not get destroyed at all – AbhishekB Aug 02 '13 at 13:48
  • It does get destroyed but never calls ondestroy or any other event when it occurs. That's why I set a pref var when it starts and then check it at start to see if I need to cleanup from the last run. – Tony Maro Aug 04 '13 at 12:05