Incorrect extras received with all intents but the

2019-04-19 22:31发布

I have a small application which can be used to set reminders for future events. The app uses an AlarmManager to set the time for when the user should be reminded. When the alarm goes off, a BroadcastReceiver registers this and in turn starts a service to notify the user via a toast and a notification in the status bar.

In order to display the correct information in the notification and toast, some extra information is passed along with the intent. The first time a reminder is registered, the info received by the BroadcastReceiver and passed on to the service is correct. But for each following reminder (i.e. each new intent received by the BroadcastReceiver) this information stays the same even when the information sent is different.

As an example, if the string "foo" is put as an extra with the first intent, "foo" is correctly extracted by the BroadcastReceiver. If "bar" is put as an extra in the second intent, "foo" is still extracted by the BroadcastReceiver.

This is the code that registers the alarm and passes the intent (main ui-class):

Intent intent = new Intent(ACTION_SET_ALARM);
intent.putExtra("desc", desc);
intent.putExtra("time", time);
intent.putExtra("dbId", dbId);
intent.putExtra("millis", millis);
PendingIntent pIntent = PendingIntent.getBroadcast(quickAlert.this, 0, intent, 0);

// Schedule the alarm!
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, millis, pIntent);

The onReceive()-method in the BroadcastReceiver class:

@Override
public void onReceive(Context context, Intent intent) {

    Intent i = new Intent(context, AlertService.class);

    String desc = intent.getStringExtra("desc").equals("") ? "": ": " + intent.getStringExtra("desc");
    String time = intent.getStringExtra("time");
    long dbId = intent.getLongExtra("dbId", -1);
    long millis = intent.getLongExtra("millis", -1);

    i.putExtra("desc", desc);
    i.putExtra("time", time);
    i.putExtra("dbId", dbId);
    i.putExtra("millis", millis);
    Log.d(TAG, "AlertReceiver: " + desc + ", " + time + ", " + dbId + ", " + millis);

    Toast.makeText(context, "Reminder: " + desc, Toast.LENGTH_LONG).show();
    context.startService(i);
}

The intent-filter in the manifest:

<receiver android:name=".AlertReceiver">
        <intent-filter>
            <action android:name="com.aspartame.quickAlert.ACTION_SET_ALARM" />
        </intent-filter>
    </receiver>

I've been stuck with this for some time now, so help is very much appreciated!

3条回答
我欲成王,谁敢阻挡
2楼-- · 2019-04-19 22:45

The above answers are correct, but they lack an explanation. It's worth noting this part of the PendingIntent documentation:

A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. ... If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token

Note that "extra" data is specifically not included in the concept of PendingIntent-identity!

查看更多
▲ chillily
3楼-- · 2019-04-19 22:47

best Link I've found on this topic: http://alvinalexander.com/source-code/android/android-how-attach-extra-intent-pendingintent-notification-solution-example

this is the clue:

int uniqueInt = (int) (System.currentTimeMillis() & 0xfffffff);
PendingIntent.getService(context, uniqueInt, intent, 0)
查看更多
欢心
4楼-- · 2019-04-19 22:59

Try adding FLAG_UPDATE_CURRENT to your PendingIntent when you create it via getBroadcast().

查看更多
登录 后发表回答