简体   繁体   中英

Repeating Alarm Notification (Android)

Will this work?

  public void setRepeatingAlarm() {

    ArrayList<Integer> AlarmDays = datasource.getDays();
    final int _id = (int) System.currentTimeMillis();

    Log.d("AlarmManageDebt", String.valueOf(AlarmDays));

    for (int i : AlarmDays) {

        Calendar cal = Calendar.getInstance();
        if (cal.get(Calendar.SECOND) >= 30)
            cal.add(Calendar.MINUTE, 1);
        cal.set(Calendar.SECOND, Integer.valueOf(i));

        Intent intent = new Intent(this, TimeAlarm.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, _id,
                intent, PendingIntent.FLAG_CANCEL_CURRENT);
        am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                60 * 1000, pendingIntent);
    }
}

I have a unique ID for the pendingIntent, and the only thing different in the alarm is the "i" variable that gives it new seconds depending on the database row (eventually, this will be a daily notification and "i" will represent a day, not a second. But this is for testing purposes. I have confirmed the correct data is being passed in "i". I just am not sure if this will create three different alarms?

So am I creating multiple alarms here? or is each loop effectively replacing the one just created?

Just based on what the docs say, I would imagine that you will only have one AlarmManager:

"If there is already an alarm for this Intent scheduled (with the equality of two intents being defined by filterEquals(Intent)), then it will be removed and replaced by this one." http://developer.android.com/reference/android/app/AlarmManager.html

And here is how Intent equality is determined: http://developer.android.com/reference/android/content/Intent.html#filterEquals(android.content.Intent )

Based on this I would say no, even though you have unique ids for the PendingIntent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM