3

I have the following broadcast receiver to capture the event when user is uninstalling app on device, technically, I am receiving intent with action ACTION_PACKAGE_REMOVED:

public class appUninstallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent)
    {
        if (intent != null) {
            if (intent.getAction().equals(intent.ACTION_PACKAGE_REMOVED))   {
                try {
                   String packageName = intent.getData().toString();
                   //Logcat shows the packageName is "com.XXX.YYY"
                   Log.v("debug",packageName);

                   PackageManager packageManager = context.getPackageManager();
                   PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
                   //Got NameNotFoundException
                   Log.v("debug",packageInfo.versionName);

                }catch(NameNotFoundException e){
                     e.printStackTrace();
                }
            }
        }
    }
}

The above receiver works well except that when it tries to extract the version name of the uninstalling app(package) with packageInfo.versionName, the NameNotFoundException is rising.

The packageName I got is "com.XXX.YYY" which is exactly the package name of the app I am uninstalling. But why I am not able to get the version name with above code?

(By the way, the above receiver is triggered when app uninstall starts, is it because the system has already removed the metadata before it starts uninstallation?)

Mellon
  • 37,586
  • 78
  • 186
  • 264
  • The doc says "An existing application package has been removed from the device". This is probably why the NameNotFoundException is rising. – Y2i Sep 19 '13 at 15:31
  • Can you please show me how you are capturing the uninstall event please..i need it – Rauf Sep 26 '13 at 09:35

3 Answers3

3

But why I am not able to get the version name with above code?

You will notice that the action name (ACTION_PACKAGE_REMOVED) is past tense. The action name is not ACTION_PACKAGE_REMOVING, ACTION_PACKAGE_WILL_BE_REMOVED_IN_THE_FUTURE, or ACTION_PACKAGE_USER_WANTS_TO_REMOVE_THIS_PACKAGE_WHICH_WE_WILL_DO_ANY_MINUTE_NOW.

The documentation also uses the past tense:

An existing application package has been removed from the device.

Hence, if the application is being uninstalled, the application does not exist on the device by the time this broadcast is sent, and therefore it will not be available via PackageManager.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • But is there other broadcast intent I could receive with which I know the app is going to be uninstalled so that I can still access the package version name? Or any workaround? – Mellon Sep 20 '13 at 07:31
  • @Mellon: "But is there other broadcast intent..." -- not related to uninstalling. "Or any workaround?" -- since I have no idea what the real business problem is, I cannot suggest a workaround. What is the point of trying to determine the version name of an app that no longer exists on the device? – CommonsWare Sep 20 '13 at 10:30
-1

Try this code to get the package name of app which is being uninstalled:

Uri uri = intent.getData();
    String pkg = uri != null ? uri.getSchemeSpecificPart() : null;
    System.out.println("Pakcge Removed: " + pkg);
rupesh
  • 2,865
  • 4
  • 24
  • 50
-1

you can use this broadcaster:

    public class UninstallHelper extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Uri uri = intent.getData();
        String packageName = uri != null ? uri.getSchemeSpecificPart() : null;
        Bundle b = intent.getExtras();
        int uid = b.getInt(Intent.EXTRA_UID);

        boolean replacing = b.getBoolean(Intent.EXTRA_REPLACING);


        Toast.makeText(context, "Package removed: " + packageName + " " + uid + "; replacing: " + replacing, Toast.LENGTH_LONG).show();


    }

}

Then in the manifest:

   <receiver android:name="braadcasters.UninstallHelper">
            <intent-filter android:priority="999">
                <action android:name="android.intent.action.PACKAGE_REMOVED" />
                <action android:name="android.intent.action.UID_REMOVED" />
                <data android:scheme="package"/>
            </intent-filter>
        </receiver>
Victor Ruiz.
  • 1,706
  • 22
  • 22