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

11,416

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

Share:
11,416
Martin
Author by

Martin

Updated on July 18, 2022

Comments

  • Martin
    Martin almost 2 years

    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

  • Martin
    Martin over 12 years
    Worked great... simply used getData().getSchemeSpecificPart(). Thanks
  • gonzobrains
    gonzobrains about 11 years
    When I print out intent.toString() I can see the package name, but when I use intent.getData().getSchemeSpecificPart() I get an empty string. What am I doing wrong?
  • Anubian Noob
    Anubian Noob almost 9 years
    @gonzobrains You probably didn't set the scheme? In the manifest, it should look like this: <data android:scheme="package" />.