Android AlarmManager - 安排一个重复的意图每天触发两次
在阅读了有关此问题的大量示例代码后,我试图找出实现以下目标的最简单方法:
我希望能够安排一个回调我的 Alarm BroadcastReceiver 的 Intent,从而触发我的服务。但是,我想进行设置,以便它每天调用所述意图两次,并且仅在尚未设置的情况下安排警报(同样用于取消警报)。
但是,我不确定以下代码是否是设置和取消警报的正确方法。
//Static function for setting the alarm
//My midday calendar object (cal1)
...
//My evening calendar object (cal2)
AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE)
Intent myIntent = new Intent(context, MyAlarmReceiver.class);
PendingIntent firstCallIntent = PendingIntent.getBroadcast(context, FIRST_CALL_ID, myIntent, PendingIntent.FLAG_NO_CREATE);
PendingIntent secondCallIntent= PendingIntent.getBroadcast(context, SECOND_CALL_ID, myIntent, PendingIntent.FLAG_NO_CREATE);
if(firstCallIntent == null){
if(DEBUG){
Log.d(TAG, "Setting Midday Alarm");
}
firstCallIntent = PendingIntent.getBroadcast(context, FIRST_CALL_ID, myIntent, 0);
alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(), AlarmManager.INTERVAL_DAY, firstCallIntent);
}
if(secondCallIntent == null){
if(DEBUG){
Log.d(TAG, "Setting Evening Alarm");
}
secondCallIntent = PendingIntent.getBroadcast(context, SECOND_CALL_ID, myIntent, 0);
alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), AlarmManager.INTERVAL_DAY, secondCallIntent);
}
//Static call to cancel the alarm.
AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE)
Intent myIntent = new Intent(context, MyAlarmReceiver.class);
PendingIntent firstCallIntent = PendingIntent.getBroadcast(context, FIRST_CALL_ID, myIntent, 0);
alarms.cancel(firstCallIntent);
firstCallIntent.cancel();
PendingIntent secondCallIntent = PendingIntent.getBroadcast(context, SECOND_CALL_ID, myIntent, 0);
alarms.cancel(secondCallIntent);
secondCallIntent.cancel();
After reading lots of sample code into this matter, I'm trying to figure out the simplest way to achieve the following:
I want to be able to schedule an Intent that calls back to my Alarm BroadcastReceiver, which in turn fires off my Service. However, I want to set up so that it calls said Intent twice a day and to only schedule the alarms if they haven't already been set (likewise for canceling the alarms).
However, I am unsure if the following code is the correct way to set and cancel alarms.
//Static function for setting the alarm
//My midday calendar object (cal1)
...
//My evening calendar object (cal2)
AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE)
Intent myIntent = new Intent(context, MyAlarmReceiver.class);
PendingIntent firstCallIntent = PendingIntent.getBroadcast(context, FIRST_CALL_ID, myIntent, PendingIntent.FLAG_NO_CREATE);
PendingIntent secondCallIntent= PendingIntent.getBroadcast(context, SECOND_CALL_ID, myIntent, PendingIntent.FLAG_NO_CREATE);
if(firstCallIntent == null){
if(DEBUG){
Log.d(TAG, "Setting Midday Alarm");
}
firstCallIntent = PendingIntent.getBroadcast(context, FIRST_CALL_ID, myIntent, 0);
alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(), AlarmManager.INTERVAL_DAY, firstCallIntent);
}
if(secondCallIntent == null){
if(DEBUG){
Log.d(TAG, "Setting Evening Alarm");
}
secondCallIntent = PendingIntent.getBroadcast(context, SECOND_CALL_ID, myIntent, 0);
alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), AlarmManager.INTERVAL_DAY, secondCallIntent);
}
//Static call to cancel the alarm.
AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE)
Intent myIntent = new Intent(context, MyAlarmReceiver.class);
PendingIntent firstCallIntent = PendingIntent.getBroadcast(context, FIRST_CALL_ID, myIntent, 0);
alarms.cancel(firstCallIntent);
firstCallIntent.cancel();
PendingIntent secondCallIntent = PendingIntent.getBroadcast(context, SECOND_CALL_ID, myIntent, 0);
alarms.cancel(secondCallIntent);
secondCallIntent.cancel();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对我来说似乎没问题,但是您可以设置时间间隔,而不是创建两个日历对象,
除非您的意图是做不同的事情。
另外,
应该足以取消这些类型的所有警报,无需:
编辑:设置 2 个日历对象
Calendar.getInstance() 将返回一个日历对象并将其设置为当前系统时间。每个 .set 方法都会更改该日历对象的某个变量。所以目前如果是晚上8点,就会把闹钟设置为当天的12点和7点,这是没有用的。因此,如果您想将其设置为第二天,则需要使用 cal1.add(Calendar.DAY_OF_MONTH, 01);添加额外的一天,设置为第二天的那个时间。希望这有帮助。
That seems alright to me, however instead of creating two calendar objects you could just set your interval to
unless your intents are doing different things.
Also,
should be enough to cancel all alarms of those types, no need for:
Edit: Setting 2 calendar objects
Calendar.getInstance() will return a calendar object and set it to the current system time. Each .set method changes a certain variable of that calendar object. So currently if it was 8pm, it would set the alarm for 12 and 7 that day, which would be no use. So if you want to set it for the next day, you'll need to use cal1.add(Calendar.DAY_OF_MONTH, 01); to add an extra day, setting for that time the next day. Hope this helps.