Reputation:
I have to show prompt when a new Application is installed or uninstalled in the device,so far its working fine. The only problem is prompt is coming even when Application is updated.
How to stop BroadCastReceiver
from triggering on Application update.
<receiver android:name=".WeepingReceiver">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
BroadCast
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_PACKAGE_INSTALL)
|| intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
context.startActivity(new Intent(context, NewAppActivity.class).
setFlags(Intent.FLAG_ACTIVITY_NEW_TASK).putExtra(Utility.NEW_PACKAGE_NAME, packageName));
}
Upvotes: 3
Views: 457
Reputation: 590
Just try to change your if condition as
if (!intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED) &&
(intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED))) {
context.startActivity(new Intent(context, NewAppActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra(Utility.NEW_PACKAGE_NAME, packageName));
}
And manifest
<receiver android:name=".WeepingReceiver">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
Upvotes: 0
Reputation: 590
Try this
Bundle extras = intent.getExtras();
if (extras.containsKey(Intent.EXTRA_REPLACING) && extras.getBoolean(Intent.EXTRA_REPLACING))
{
//do nothing here it is condition of updating your existing app
}else
{
//do your code here
}
Upvotes: 3
Reputation: 724
Application Update is re-installing a new app so it is correct that your receiver receives the event as PACKAGE_ADDED
. Thus, you cannot stop your broadcast from receiving the event.
However, you can validate if the intent is being updated by checking whether the package name was there before. You can have a list of installed apps' package names and store. Then check as you are doing:
if (intent.getAction().equals(Intent.ACTION_PACKAGE_INSTALL)
|| intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)
|| !packageList.contains(packageName))
You could get your package list by:
final PackageManager pm = getPackageManager();
//get a list of installed apps.
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
packageList.add(packageInfo.packageName);
}
Upvotes: 0