androidandroid-intentbroadcastreceiverandroid-broadcastandroid-package-managers

How to find the package name which has been uninstalled when using Intent.ACTION_PACKAGE_REMOVED


I have an application which is keeping a log of internally developed applications installed on the device. Upon installation a broadcast receiver for Intent.PACKAGE_ADDED is invoked and records the package name using the following code:

public class NewInstallReceiver extends BroadcastReceiver 
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Bundle b = intent.getExtras();
        int uid = b.getInt(Intent.EXTRA_UID);
        String[] packages = context.getPackageManager().getPackagesForUid(uid);

        ApplicationService appService = new ApplicationService(context);
        appService.ApplicationInstalled(packages);
    }
}

The problem I'm facing is when using a broadcast receiver for Intent.PACKAGE_REMOVED, all reference to the package via the unique Id (UID) comes back with null information (As you would expect, given its already been uninstalled). I have a temporary solution for the meantime, but its not very elegant, and for the next version I would like to have cleaner code. An example of how the code should work:

public class RemoveApplicationReceiver extends BroadcastReceiver 
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        Bundle b = intent.getExtras();
        int uid = b.getInt(Intent.EXTRA_UID);
        String[] packages = context.getPackageManager().getPackagesForUid(uid);

        ApplicationService appService = new ApplicationService(context);
        appService.ApplicationRemoved(packages);
    }

}

So to recap, the question is:

How, after a program has been removed, can I reference the package name in a broadcast receiver for Intent.PACKAGE_REMOVED.

Thanks


Solution

  • The package names are in the Intent you got from BroadcasReceiver, use the "getData()" function, there is the ComponentMame of the installed/uninstalled package.