6

I want to clean up the junk created by my application at time on UnInstalling the Application.

Using ManiFest File:-

Added in Manifest File:

 <receiver android:name="com.netdoers.com.ui.CleanReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_REMOVED" >
            </action>
            <data android:scheme="package"/>
        </intent-filter>
    </receiver>

Created Receiver to catch the BroadCast Event

public class CleanReceiver extends BroadcastReceiver
{
  public void onReceive(Context context, Intent intent) {
    CustomToast.showToastMessage(context, "Uninstalling Application");
    Log.e("Uninstall", "CleanReceiver Called");
  }
} 

In Java Code:-

 BroadCastReceiver br = new CleanReceiver();
 IntentFilter intentFilter = new IntentFilter();
 intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
 intentFilter.addDataScheme("package");
 registerReceiver(br, intentFilter);

But at time of Uninstalling application the receiver is never been called.

Both Java and Manifest never call Receiver at event of Uninstall application. How to catch the broadcast event at time of Uninstalling application?

Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96

1 Answers1

8

You can get the broadcast for any other package getting uninstalled but never for your own package.

Reason

That happens because when you register uninstall receiver in your own app and when the app is uninstalled, the registered BroadcastReceiver has been uninstalled before the app gets uninstalled,so its own uninstallation event won't be received by that BroadcastReceiver.

Just think of a scenario that say a broadcast is registered(say a sms receiver) and the app is about to get uninstalled.Now sms comes in broadcast detects it but the broadcast's application(which created it) got uninstalled.The may lead to inconsistency in the system.So may be thats the reason it happens.

cafebabe1991
  • 4,928
  • 2
  • 34
  • 42
  • So how to prevent these kind of scenarios? I'm stuck up with these since way long. As I'm using Manifest file to register broadcast then how come it got uninstall before actual application get uninstalled? – Vikalp Patel Feb 24 '14 at 13:59
  • Just think of a scenario that say a broadcast is registered(say a sms receiver) and the app is about to get uninstalled.Now sms comes in broadcast detects it but the broadcast's application(which created it) got uninstalled.The may lead to inconsistency in the system.So may be thats the reason it happens. – cafebabe1991 Feb 24 '14 at 14:03
  • How to avoid such scenarios then? Is it possible to broadcast event at time of Uninstalling application as it's system protected broadcast? – Vikalp Patel Feb 24 '14 at 14:25
  • Its not possible for your own app to realize its own uninstallation.For any other application getting uninstalled it can be useful. – cafebabe1991 Feb 24 '14 at 14:34
  • Is it anyway to clean the junk which my application created on SD Card? – Vikalp Patel Feb 24 '14 at 14:40
  • Yes that gets handled automatically but to take advantage of that store your data in this location. /mnt/sdcard/Android/data/com.yourapp.com . As android deletes everything inside this location after uninstallation of your application. – cafebabe1991 Feb 24 '14 at 14:43
  • https://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_REMOVED – ashishdhiman2007 May 13 '22 at 12:19