How to Make My App Receive Broadcast When Other Applications Are Installed or Removed

How to make my app receive broadcast when other applications are installed or removed

In your manifest:

<receiver android:name=".apps.AppListener">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
</intent-filter>
</receiver>

Add the line before the intent-filter tag

<data android:scheme="package"/>

So your manifest should look like this:

<receiver android:name=".apps.AppListener">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>

Am not sure about the PACKAGE_REMOVED intent in that if its actually is available.

Android get broadcast about the application installed or removed

Intent that you get in onReceive function contains the information related to the package being added or removed.

intent.getData().toString()

You can get the application name by this function:

  private String getApplicationName(Context context, String data, int flag) {

final PackageManager pckManager = context.getPackageManager();
ApplicationInfo applicationInformation;
try {
applicationInformation = pckManager.getApplicationInfo(data, flag);
} catch (PackageManager.NameNotFoundException e) {
applicationInformation = null;
}
final String applicationName = (String) (applicationInformation != null ? pckManager.getApplicationLabel(applicationInformation) : "(unknown)");

return applicationName;
}

For more info check:
Created BroadcastReceiver which displays application name and version number on install/ uninstall of any application?

Android Broadcastreceiver for other apps install/delete not working

You are trying to listen to broadcasts like ACTION_PACKAGE_ADDED and ACTION_PACKAGE_REPLACED. That is fine for Android 7.1 and lower. On Android 8.0+, you cannot register for those broadcasts in the manifest, as most implicit broadcasts are banned.

Instead, you need to call getChangedPackages() on PackageManager periodically, such as via WorkManager. This will not give you real-time results, but real-time results are no longer an option on Android 8.0+.

Listen for app installed / upgraded broadcast message in Android

If you are installing an application A, all other applications on the device will get the Intent that application A is the newly installed application but not A itself as it doesn't seem of any use. Now A will get broadcasts if other apps are later installed or changed.


If you want to find out at when your app was installed or some other app's the last install or update time, you can always use PackageManager:

PackageManager pm = context.getPackageManager();
ApplicationInfo appInfo = pm.getApplicationInfo("app.package.name", 0);
String appFile = appInfo.sourceDir;
long installed = new File(appFile).lastModified();

here app.package.name is the package name of the app you want to find out the install time. If you want to use it for your app, pass in your app's package name.

Android: How to get the installed App information using Broadcast receiver

All the information you want is in the Intent extras.

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

Receiving package install and uninstall events

I tried to register the BroadcastReceiver in either manifest file or java code. But both of these two methods failed to trigger the onReceive() method.
After googling this problem, I found a solution for both methods from another Thread in SO:
Android Notification App

In the manifest file (this approach no longer applies since API 26 (Android 8), it was causing performance issues on earlier Android versions):

<receiver android:name=".YourReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>

In java code:

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL);
intentFilter.addDataScheme("package");
registerReceiver(br, intentFilter);

This should work for you.

If My application Installed in device then after Any Application install from play store then show Notification using my application

That was possible before Android 8.0 (API level 26), Check this answer

<receiver android:name=".apps.AppListener">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>

After android 8.0 from docs:

Apps that target Android 8.0 or higher can no longer register
broadcast receivers for implicit broadcasts in their manifest. An
implicit broadcast is a broadcast that does not target that app
specifically. For example, ACTION_PACKAGE_REPLACED is an implicit
broadcast, since it is sent to all registered listeners, letting them
know that some package on the device was replaced. However,
ACTION_MY_PACKAGE_REPLACED is not an implicit broadcast, since it is
sent only to the app whose package was replaced, no matter how many
other apps have registered listeners for that broadcast.

Workaround by CommonsWare, sample app

You can also use polling, setting up a JobScheduler job to check every
so often, asking PackageManager for what has changed in the roster of
installed apps via getChangedPackages():

How to get name of installed or removed application?

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



Related Topics



Leave a reply



Submit