33

I have developed an Android app. Now I want to perform a few operations (i. e. - Reset the settings etc.. ) at the moment the app gets uninstalled from the phone.

Is it possible to reigster a listener or a function that is called at the moment the app is removed?

Janusz
  • 187,060
  • 113
  • 301
  • 369
dhaiwat
  • 1,731
  • 4
  • 16
  • 16
  • 1
    The application `NQ Mobile Security` is calling an Activity at uninstall look at the http://i.imgur.com/Fos9N.png, http://i.imgur.com/fIZbK.png, http://i.imgur.com/cG9Hr.png and the question http://stackoverflow.com/questions/10219328/how-to-show-an-activity-before-my-app-is-uninstalled-android – Gaurav Agarwal Jun 16 '12 at 10:17
  • Check my answer at : http://stackoverflow.com/questions/5132472/can-code-be-called-when-my-android-application-is-uninstalled/28209337#28209337 this will help you – A-Droid Tech Jan 29 '15 at 07:44

4 Answers4

22

Sadly android at the moment does not give you a possibility to perform code at the moment your app is uninstalled.

All the settings that are set via the SharedPreferences are deleted together with everything in the Application Data an Cache folder.

The only thing that will persist is the data that is written to the SD-Card and any changes to phone settings that are made. I don't know what happens to data that is synchronized to the contacts through your app.

nobalG
  • 4,544
  • 3
  • 34
  • 72
Janusz
  • 187,060
  • 113
  • 301
  • 369
  • 9
    I don't think its sad. In fact, I am elated, because it helps prevent spyware-like activity on my phone. – Brad Hein Jun 10 '10 at 13:02
  • 5
    But it also prevents me from cleaning up certain things like the data I downloaded to SD-Card to prevent the phone memory from filling up. No I fill up the SD-Card. Maybe this will get better with app to sd in froyo but for devices under 2.2 I would like to be that polite on a uninstall. – Janusz Jun 10 '10 at 13:07
  • 7
    @Janusz: Take a look at `getExternalStorageDirectory()` on the `android.os.Environment` class. The online doc says that has been there since API level 1, but I do not remember it. Regardless, that is supposed to give you an app-specific directory on the SD card that will be cleaned up when the app is uninstalled. – CommonsWare Jun 10 '10 at 13:28
  • @dhaiwat - Time to accept your first answer. Go ahead, press the green checkbox, it won't bite. – Brad Hein Jun 10 '10 at 14:52
  • This can be possible but i dont know how.In the market there is an application named as "Application Protector" who do this job. when user try to uninstall the application then user has to enter the password which is custom layout of the application. – Amit Thaper May 10 '11 at 12:51
  • 2
    The application `NQ Mobile Security` is calling an Activity at uninstall look at the http://i.imgur.com/Fos9N.png, http://i.imgur.com/fIZbK.png, http://i.imgur.com/cG9Hr.png and the question http://stackoverflow.com/questions/10219328/how-to-show-an-activity-before-my-app-is-uninstalled-android – Gaurav Agarwal Jun 16 '12 at 10:18
  • If it isnt possible then how Firebase Analytics provides app_remove events automatically? – Ofek Ron Jul 28 '16 at 11:52
  • @OfekRon of course it is possible to detect and google is doing it (sad for @BradHein) , but the question is whether the app can run code when it is being uninstalled. – nurettin Jan 08 '17 at 11:56
  • What about alarms set in `AlarmManager` ? – Talha Sep 07 '17 at 12:58
12

You cannot get control when your application is uninstalled -- sorry!

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    thanks, i set receiver for android.intent.action.PACKAGE_REMOVED and it called when any application uninstall now how can i get package name which has been deleted on this receiver – dhaiwat Jun 11 '10 at 13:04
  • If it isnt possible then how Firebase Analytics provides app_remove events automatically? – Ofek Ron Jul 28 '16 at 11:52
  • 2
    @OfekRon: Please do not post duplicate comments on multiple answers. I do not use Firebase, and I do not know what "app_remove events" are. However, *other apps* on the device can find out that your app was uninstalled. So, I would presume that the Play Services Framework app, the Play Store, or some other piece of Google proprietary code, is the one that is detecting the uninstall and forwarding that information to Google's servers. – CommonsWare Jul 28 '16 at 12:24
  • @CommonsWare they are firebase analytics events. Google generates and shows you conversion statistics using those events. You can also generate custom events (with up to 500 different event types, I think) – nurettin Jan 08 '17 at 11:59
  • Only thing you can do is to use any push notification service. – Md omer arafat Feb 06 '23 at 11:36
3

Since API level 8 you may use Context.getExternalFilesDir(). In theory any data placed here will be removed when the application is uninstalled.

http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)

jasonhudgins
  • 2,815
  • 1
  • 25
  • 20
-3

When user uninstall android application in his mobile the PACKAGE_REMOVED receiver will call. You can get uninstalled app package name use intent,getDataString()

if (intent.getAction (). equals ("android.intent.action.PACKAGE_REMOVED")) {
          String packageName = intent.getDataString ();
          System.out.println ("uninstall:" + packageName + "package name of the program");  
}

See complete example in this link. http://foryouneed.blogspot.in/2014/08/android-listener-application-to-install.html

Valery Viktorovsky
  • 6,487
  • 3
  • 39
  • 47
  • 3
    The app being uninstalled will not receive this Intent, from docs: "The package that is being installed does not receive this Intent". http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_REMOVED – some.birdie Sep 26 '14 at 12:47