2

I'm developing an Android service. When the service is stopped, onDestroy is called as expected. However, when I uninstall the app while the service is running, onDestroy is not called, leaving the app no chance to clean-up itself.

Is this normal? How can I perform shutdown/cleanup logic for the service when the app is uninstalled?

Sam
  • 40,644
  • 36
  • 176
  • 219
  • 1
    Kind of the same situation appears when you remove app from **Recents**. Nor the Application or Activities/Services `onDestroy` method won't called. I think it's normal, just keep in your mind that service is not existing when you uninstall the app. – romtsn Nov 07 '14 at 07:29

2 Answers2

6

Android specifically does not allow an app to be notified when it is uninstalled (think of what a malicious app would do - re-install, SPAM the user with pop-ups, etc.)

If you are concerned with "clean up" then keep all your files in your private app space (i.e. do not use the SD Card). Otherwise, you must expect the user to clean up.

There are no other options.


See these related posts about uninstall:

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36
  • It looks like you're right about this not being possible, but there's more to cleanup then just deleting files. In my case, I'm developing a custom rotation manager, and I need to be able to revert the screen orientation settings on uninstall to prevent the screen from being semi-stuck in the wrong orientation. – Sam Mar 30 '15 at 09:00
  • I don't agree with your justification; there's plenty of stuff out there that can be abused that is still allowed. Examples: reading contacts, changing system settings, random advertisement popups, secretly taking photos, using accounts, reading messages, and others, so saying "it can be abused by a malicious app" seems inconsistent. Regarding the app reinstalling itself when uninstalled, does Android even allow apps to silently install apps? – Sam Mar 30 '15 at 09:01
  • 1
    First, no - an app cannot install another app without the user's knowledge (at best). Second, the ability to do something malicious does not mean the user cannot give that permission to apps. Most of the things that you mentioned are "permission based" actions. My response was to illustrate, not to dictate policy. The app Lookout used to take advantage of a bug that allowed them to detect an uninstall - with no malicious intent or action. To my knowledge, nothing malicious ever happened. Android (Google) "fixed" the bug... – Jim Mar 31 '15 at 03:11
2

You cannot guarantee that onDestroy will be called in all cases.

When an Android app is stopped by the system, the app is not shut down gracefully. Instead it is much simpler to just kill the process that it is running in - basically shutting down the Dalvik instance right away. This is an optimisation that is made possible by the sandboxing of placing separate apps in their own runtime instances.

That is why onDestroy will not be called - the app is stopped dead in its tracks.


In addition, as mentioned in Jim's good explanation, apps cannot be notified of their own uninstallation.

Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255