I want to develop an app that can receive broadcast about the other apps being installed or removed. So far, I tried the code below but, it only provides a broadcast event when an installation on deletion occurs, it does not provide info about the other apps beibg installed or removed. So, is there a way to get the package name of the newly installed application.
in manifset:
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>
in AppListener:
public class AppListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// there is a broadcast event here
// but how to get the package name of the newly installed application
Log.v(TAG, "there is a broadcast");
}
}
Addition: This is deprecated for Api 14 or upper.
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
Package name is embedded into the Intent you are receiving in onReceive() method. You can read it using below code snippet :
Uri data = broadcastIntent.getData();
String installedPackageName = data.getEncodedSchemeSpecificPart();
For PACKAGE_ADDED, PACKAGE_REMOVED and PACKAGE_REPLACED, you can get package name using above code.
In case of application update, you will get 2 broadcasts back to back as below : 1. PACKAGE_REMOVED 2. PACKAGE_REPLACED
In case of application update, PACKAGE_REMOVED intent will contain extra boolean to differentiate between app removal and app update. You can read this boolean as below :
boolean isReplacing = broadcastIntent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Just to get package name you are calling PackageManagerService api is overhead. Must avoid it.
Hope, this will help you.
There are 2 options:
1) If you are asking if you can receive a broadcast when your same app is being uninstalled, then the answer is:
This cannot be done, unless you are a System app.
Android does not notify the app when it is going to be installed. This would be a security risk, as it would enable apps to prevent uninstallation.
2) If you are asking about when other apps are being uninstalled, then this might be a duplicate of:
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?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.