0

I have written a broadcast receiver to detect installing and removing applications. But I want to get the name of installed or removed application too. How can I do that?

This is my BroadcastReceiver:

public class PackageReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        switch(intent.getAction())
        {
            case Intent.ACTION_PACKAGE_ADDED:
                String replaced = "";
                if(intent.getBooleanExtra(Intent.EXTRA_REPLACING, false))
                {
                    replaced = "replaced";
                }
                Log.e("application", "installed " + replaced);

                break;

            case Intent.ACTION_PACKAGE_REMOVED:
                if(!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false))
                {
                    Log.e("application", "removed");
                }

                break;
        }

    }
}

In the manifest:

<receiver
    android:name=".receivers.PackageReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
         <action android:name="android.intent.action.PACKAGE_ADDED" />
         <action android:name="android.intent.action.PACKAGE_REMOVED" />
         <data android:scheme="package" />
    </intent-filter>
</receiver>
MR.Iraji
  • 56
  • 9
  • Follow this tutorial:- http://theopentutorials.com/tutorials/android/listview/how-to-get-list-of-installed-apps-in-android/ – Mahesh Vayak Jan 05 '18 at 09:39
  • https://stackoverflow.com/questions/18692571/how-can-an-app-detect-that-its-going-to-be-uninstalled – Goku Jan 05 '18 at 09:47

1 Answers1

1

Combining info found in android documentation and this answer on stack overflow I came up with the following. Intents you are using might have EXTRA_UID extra which contains uid of modified app. With uid you can get the app name. However you can only do that on ACTION_PACKAGE_ADDED intent, because in ACTION_PACKAGE_REMOVED app was already remove and you cant get its name (you can still get uid).

Check this sample:

int uid = intent.getIntegerExtra(Intent.EXTRA_UID);
String appName = context.getPackageManager().getNameForUid(uid);

So in your case it would be:

public class PackageReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        switch(intent.getAction())
        {
            case Intent.ACTION_PACKAGE_ADDED:
                String replaced = "";
                String appName = "";
                int uid = -1;
                if(intent.getBooleanExtra(Intent.EXTRA_REPLACING, false))
                {
                    replaced = "replaced";
                }
                uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
                if(uid != -1){
                    appName = context.getPackageManager().getNameForUid(uid);
                }
                Log.e("application", "installed " + replaced + " uid " + uid + " appname " + appName);

                break;

            case Intent.ACTION_PACKAGE_REMOVED:
                if(!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false))
                {
                    Log.e("application", "removed");
                }
                break;
        }
    }
}

With this code I've seen this written in logcat after installing Google Earth from Google Play:

installed uid 10404 appname com.google.earth

Mateusz
  • 681
  • 5
  • 9
  • The input of getNameForUid method is an int not String. – MR.Iraji Jan 05 '18 at 10:00
  • 1
    However I tried this: int installedUid = intent.getIntExtra(Intent.EXTRA_UID, -1); String installedAppName = context.getPackageManager().getNameForUid(installedUid); and it returns null – MR.Iraji Jan 05 '18 at 10:01
  • @MR.Iraji See if String returned by intent.getStringExtra(Intent.EXTRA_UID) is a number, if so try parsing it to a int with `Integer.parseInt(uid)`. – Mateusz Jan 05 '18 at 10:03
  • It is write in developer.android.com: EXTRA_UID containing the integer uid previously assigned to the package. However as I said before, I have tried it and getNameForUid(uid) method returned null to me. – MR.Iraji Jan 05 '18 at 10:11
  • Yes, you are right. I will now try do research this problem further. Wait for my answer. – Mateusz Jan 05 '18 at 10:15
  • @MR.Iraji I've updated my answer. This method doesn't work on app removal, but I don't think there is method that works. With this you get package name. Tell me if you want name from launcher and I will add it to my answer – Mateusz Jan 05 '18 at 10:33
  • I now understand that if I install an application this code returns package name of installed application. But if I remove an application this code returns null to me because after removing an application, the package name of application doesn't exist anymore. So getNameForUid(uid) method returns null to me. – MR.Iraji Jan 05 '18 at 10:36
  • Yes, package doesnt exists so you can't get it's name. – Mateusz Jan 05 '18 at 10:37
  • So I need to get name of application just before it removes. do you know how to do it? – MR.Iraji Jan 05 '18 at 10:39
  • If you know the answer please tell me. however you brought me closer to the answer. thank you very much. – MR.Iraji Jan 05 '18 at 10:47
  • You can get all apps(with names and uids) on first application run, and keep track which application was added/removed based on uid. – Mateusz Jan 05 '18 at 12:52