I have an BroadcastReceiver for handling new or modified applications:
<receiver android:name=".PackageHandler" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_CHANGED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
When an intent is received, I show a notification containing some of the package information. My problem is that when I add a new app, for example while debugging, two intents are received from PackageHandler, and thus two notifications are displayed. To illustrate, this BroadcastReceiver, which handles the intents discussed, will show a logcat entry every time the intent is sent, which is twice when I update an app:
public class PackageHandler extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("", "Intent Received: " + intent.getAction());
if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
Log.i("Intent Received", intent.getAction());
}
else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
Log.i("Intent Received", intent.getAction());
}
else if (intent.getAction().equals(Intent.ACTION_PACKAGE_CHANGED)) {
Log.i("Intent Received", intent.getAction());
}
}//onReceive
}//PackageHandler
However I need a solution that keeps track of having already showed a log message (from handling a different intents). From a notifications view point, I could also optionally remove extra notifications after they are displayed. Does anyone have a working solution? Note that I am supporting API 1.5.
The solution I came up with works really nicely, but I had a tough time figuring it out. In a separate class, which contains many static variables (let’s call it S.java), I created an
integercalled pid. Then I added to myBroadcastReceiverto include asynchronizedmethod that compares process PIDs and decides if one is a match. Most of the time, only one of theintentsare received, however I have had up to two, which this protects against. I have never had threeintentcalls to this receiver at one time, so this is not designed to handle that many. The new code (adding to the above code) is as follows: