views:

173

answers:

2

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!

+1  A: 

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

CommonsWare
Excellent, thank you!
aspartame
A: 

Note that you might want to also evaluate FLAG_CANCEL_CURRENT if you are concerned about outstanding entities.

BK