Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect extras received with all intents but the first one

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!

like image 972
aspartame Avatar asked Oct 04 '09 18:10

aspartame


People also ask

What are intents what are types of intents what is function of intent filter?

An intent filter is an expression in an app's manifest file that specifies the type of intents that the component would like to receive. For instance, by declaring an intent filter for an activity, you make it possible for other apps to directly start your activity with a certain kind of intent.

What is the function of an intent filter?

An intent filter declares the capabilities of its parent component — what an activity or service can do and what types of broadcasts a receiver can handle. It opens the component to receiving intents of the advertised type, while filtering out those that are not meaningful for the component.


2 Answers

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

like image 133
CommonsWare Avatar answered Sep 30 '22 20:09

CommonsWare


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!

like image 22
Jon Shemitz Avatar answered Sep 30 '22 20:09

Jon Shemitz