Pergunta

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.

Foi útil?

Solução 2

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 integer called pid. Then I added to my BroadcastReceiver to include a synchronized method that compares process PIDs and decides if one is a match. Most of the time, only one of the intents are received, however I have had up to two, which this protects against. I have never had three intent calls 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:

public class PackageHandler extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (!getPID()) {
            return;
        }
        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

    public synchronized boolean getPID() {
       ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
       List<RunningAppProcessInfo> raps = am.getRunningAppProcesses();
       int pid = 0;
       for (RunningAppProcessInfo rap : raps) {
           if (rap.processName.equals("com.example.android")) {//change to your package
               pid = rap.pid;
               break;
           }
       }
       if (pid == S.pid) {
           Log.i("", "PIDs match");
           S.pid = 0;
           return false;
       }
       else {
           Log.i("", "Non-Matching PIDs");
           S.pid = pid;
           return true;
       }
   }//getPID

}//PackageHandler

Outras dicas

If we use the same unique id in NotificationManager.notify, the new notification will replace the older notification.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top