Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

android - Is it possible to create multiple PendingIntents with the same requestCode and different extras?

I'm using AlarmManager to schedule anywhere between 1 and 35 alarms (depending on user input). When the user requests to schedule new alarms, I need to cancel the current alarms, so I create all of my alarms with the same requestCode, defined in a final variable.

// clear remaining alarms
Intent intentstop = new Intent(this, NDService.class);
PendingIntent senderstop = PendingIntent.getService(this,
            NODIR_REQUESTCODE, intentstop, 0);
am.cancel(senderstop);

// loop through days
if (sched_slider.getBooleanValue())
for (int day = 1; day < 8; day++) {

    if (day == 1 && sun.isChecked())
                scheduleDay(day);
    if (day == 2 && mon.isChecked())
                scheduleDay(day);
    if (day == 3 && tue.isChecked())
                scheduleDay(day);
    if (day == 4 && wed.isChecked())
                scheduleDay(day);
    if (day == 5 && thu.isChecked())
                scheduleDay(day);
    if (day == 6 && fri.isChecked())
                scheduleDay(day);
    if (day == 7 && sat.isChecked())
                scheduleDay(day);
}

...

public void scheduleDay(int dayofweek) {
    Intent toolintent = new Intent(this, NDService.class);
    toolintent.putExtra("TOOL", "this value changes occasionally");
    PendingIntent pi = PendingIntent.getService(this,
                NODIR_REQUESTCODE, toolintent, 0);
    calendar.set(Calendar.DAY_OF_WEEK, dayofweek);
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.SECOND, 0);
    am.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY * 7, pi);
}

Here, if the user has sun (which is a CheckBox) checked, it will schedule an alarm to run every Sunday at hour and minute. You can see that every alarm created this way has the same requestCode, but the TOOL extra changes sometimes for each alarm.

However, in my testing, when the alarm goes off and my Service runs, the extras from the Intent are now null. This question suggests that using PendingIntent.FLAG_CANCEL_CURRENT will solve this, but wouldn't that cancel the other PendingIntents?

In short:

Can someone explain how PendingIntents work, in reference to creating multiple ones with the same requestCode and different extras? What flags (if any) should I be using?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Actually, you don't "create" PendingIntents. You request them from the Android framework. When you request a PendingIntent from the Android framework, it checks to see if there is already a PendingIntent that matches the criteria you pass as arguments. If so, it does not create a new PendingIntent, it just gives you back a "token" that points to the existing PendingIntent. If it doesn't find a matching PendingIntent, it will create one and then give you back a "token" that points to the one it just created. There are some flags that you can set to modify this behaviour, but not that much. The most important thing to understand here is the way the Android framework does the matching.

To do this it checks if the following parameters match (comparing the existing PendingIntent with the parameters you have passed):

  • Request codes must be the same. Otherwise they do not match.
  • The "action" in the Intent must be the same (or both null). Otherwise they do not match.
  • The "data" in the Intent must be the same (or both null). Otherwise they do not match.
  • The "type" (of the data) in the Intent must be the same (or both null). Otherwise they do not match.
  • The "package" and/or "component" in the Intent must be the same (or both null). Otherwise they do not match. "package" and "component" fields are set for "explicit" Intents.
  • The list of "categories" in the Intent must be the same. Otherwise they do not match.

You should notice that "extras" is not in the list above. That means that if you request a PendingIntent the "extras" are not taken into consideration when the Android framework tries to find a matching PendingIntent. This is a common mistake that developers make.

We can now address the additional flags that you can add to modify the behaviour of a PendingIntent request:

FLAG_CANCEL_CURRENT - When you specify this flag, if a matching PendingIntent is found, that PendingIntent is cancelled (removed, deleted, invalidated) and a new one is created. This means that any applications that are holding a "token" pointing to the old PendingIntent will not be able to use it, because it is no longer valid.

FLAG_NO_CREATE - When you specify this flag, if a matching PendingIntent is found, a "token" pointing to the existing PendingIntent is returned (this is the usual behaviour). However, if no matching PendingIntent is found, a new one is not created and the call just returns null. This can be used to determine if there is an active PendingIntent for a specific set of parameters.

FLAG_ONE_SHOT - When you specify this flag, the PendingIntent that is created can only be used once. That means that if you give the "token" for this PendingIntent to multiple applications, after the first use of the PendingIntent it will be canceled (removed, deleted, invalidated) so that any future attempt to use it will fail.

FLAG_UPDATE_CURRENT - When you specify this flag, if a matching PendingIntent is found, the "extras" in that PendingIntent will be replaced by the "extras" in the Intent that you pass as a parameter to the getxxx() method. If no matching PendingIntent is found, a new one is created (this is the normal behaviour). This can be used to change the "extras" on an existing PendingIntent where you have already given the "token" to other applications and don't want to invalidate the existing PendingIntent.

Let me try to address your specific problem:

You cannot have more than one active PendingIntent in the system if the request code, action, data, type and package/component parameters are the same. So your requirement to be able to have up to 35 active PendingIntents all with the same request code, action, data, type and package/component parameters, but with different "extras", is not possible.

I would suggest that you either use 35 different request codes, or create 35 different unique "action" parameters for your Intent.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...