简体   繁体   English

AlarmManager设置两次警报,而不是更新当前警报

[英]AlarmManager sets alarm twice instead of updating current one

am setting an alarm at midnight and sometimes I set alarm again with the same id so why does alarm manager create another alarm instead of updating the present. 在午夜设置警报,有时我用相同的ID再次设置警报,所以为什么警报管理器会创建另一个警报而不是更新当前警报。 I end up with 2 alarms instead 1. how can I change that so if this code executes while a current alarm is present it will update it instead creating a duplicate 我最终得到2条警报,而不是1条。我该如何更改,因此,如果在当前警报出现时执行此代码,它将对其进行更新,而不是创建一个重复项

    Intent intent = new Intent(con,MyReceiver.class);
    intent.setAction("ACTIVATE_MIDNIGHT_SERVICE");
    PendingIntent pendingIntent =  PendingIntent.getBroadcast(
            con, -1, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    Date d = new Date();
    d.setDate(d.getDate()+1);
    d.setHours(0);
    d.setMinutes(0);
    d.setSeconds(0);  
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, d.getTime(),AlarmManager.INTERVAL_DAY, pendingIntent);

I just had to deal with this same problem this week. 我本周只需要处理同样的问题。 At it's basic, the AlarmManager does 2 things: 1. sets a wake up reminder in the system's memory, so the device will wake up and perform a task, and 2. calls your app (via intent) to perform a task at a specific time. 从根本AlarmManagerAlarmManager执行2件事:1.在系统内存中设置唤醒提醒,因此设备将唤醒并执行任务,并且2.调用您的应用程序(通过意图)在特定位置执行任务时间。

With that being said, when you set up an Alarm you need to specify both parts, when you want the alarm to go off (via setRepeating ), and what to do (via Intent ) 话虽如此,当您设置Alarm您需要指定两个部分,何时要发出警报(通过setRepeating ),以及要做什么(通过Intent )。

Notice that there is no indication in the setRepeating call to let the alarm manager know what to do if this call already exists, that is because you are allowed to create as many alarms as you need. 请注意,在setRepeating调用中没有指示让警报管理器知道如果该调用已存在该怎么办,这是因为允许您创建所需数量的警报。 PendingIntent.FLAG_CANCEL_CURRENT only tells the system what to do in case that the previous Intent is still active. PendingIntent.FLAG_CANCEL_CURRENT仅告诉系统在先前的Intent仍处于活动状态的情况下该怎么做。 Instead you need to cancel your existing alarm and set a new one. 相反,您需要取消现有警报并设置一个新警报。 You do that by setting the request code to something distinct (ie a positive integer) and then cancelling when you set a new alarm 您可以通过将请求代码设置为不同的值(即正整数),然后在设置新警报时取消该操作来做到这一点

Intent intent = new Intent(con,MyReceiver.class);
intent.setAction("ACTIVATE_MIDNIGHT_SERVICE");
PendingIntent pendingIntent =  PendingIntent.getBroadcast(
            con, 300, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Date d = new Date();
d.setDate(d.getDate()+1);
d.setHours(0);
d.setMinutes(0);
d.setSeconds(0);  

// This will cancel the existing alarm
alarm.cancel(pendingIntent);

alarm.setRepeating(AlarmManager.RTC_WAKEUP, d.getTime(),AlarmManager.INTERVAL_DAY, pendingIntent);

更换PendingIntent.FLAG_CANCEL_CURRENTPendingIntent.FLAG_UPDATE_CURRENT创建时PendingIntent

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

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