简体   繁体   English

重复警报通知(Android)

[英]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? 我有一个未定的ID用于endingIntent,并且警报中唯一不同的是“ i”变量,该变量根据数据库行为它赋予新的秒数(最终,这将是每日通知,而“ i”代表一天) ,而不是一秒钟。但这只是出于测试目的,我已经确认在“ i”中传递了正确的数据。我不确定这是否会产生三个不同的警报?

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: 仅根据文档的内容,我可以想象您只有一个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." “如果已为此计划调度了一个警报(两个意图的相等性由filterEquals(Intent)定义),则将其删除并替换为该警报。” http://developer.android.com/reference/android/app/AlarmManager.html 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 ) 这是确定Intent相等性的方法: 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. 基于此,即使您有PendingIntent的唯一ID,我也不会说。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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