12

When my android app is removed, I would like to also remove files the app has created on the SD card, as these can consume many megabytes and are only of use to my app.

It seems that receiving the PACKAGE REMOVED intent would be the place to do this. However, my broadcast receiver is never called--it seems to have been deleted before the PACKAGE REMOVED intent is sent

The code is:

public class UninstallReceiver extends BroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {
  String action= intent.getAction();
  Log.i("U", "ACTION " + action);
  etc.
 }
}

and, in the manifest:

 <application android:debuggable="true"
  android:icon="@drawable/icon"
  android:label="@string/app_name">

  <receiver android:name ="com.boom.UninstallReceiver">
   <intent-filter>
        <action android:name="android.intent.action.PACKAGE_REMOVED"/> 
     <data android:scheme="package" />
   </intent-filter>
  </receiver>
hexatron
  • 211
  • 1
  • 2
  • 4

3 Answers3

13

The documentation says:

The package that is being removed does not receive this Intent.

Android 2.2 added getExternalFilesDir(), which will point to a place on the external storage that Android will automatically clean up when your application is uninstalled. However, that is only for Android 2.2, and there are indications that it does not work particularly well at the moment. However, it is something to keep in mind for 2011.

Beyond that, all you can really do is offer a menu choice somewhere for the user to do the cleanup, and hope users use it before uninstalling you.

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

Have you tried this?

    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    filter.addDataScheme("package");
    registerReceiver(new UnInstalledReceiver(), filter);

Also, putting the files of your app in the folder android/data/com.boom/ is a nice easy way to make sure extra files get cleaned up when users uninstall.

Anže Mur
  • 1,545
  • 2
  • 18
  • 37
REInVent
  • 103
  • 7
-7

You must add the permission in the manifast.

<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REMOVED" />
DrFred
  • 461
  • 3
  • 6
  • 2
    This permission is only granted to system apps. So I can't use it in my app. – Borys Jun 07 '13 at 15:39
  • 4
    This answer is so wrong. This permission is required in order to **send the broadcast Intent `PACKAGE_REMOVED`**. Even if the system would grant you this permission (which it won't), it still wouldn't help. This answer should be deleted. – David Wasser Apr 12 '15 at 11:09