Android AlarmManager-安排定期触发每天两次触发 [英] Android AlarmManager - Scheduling a recurring Intent to fire off twice a day

查看:486
本文介绍了Android AlarmManager-安排定期触发每天两次触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读了大量示例代码之后,我试图找出实现以下目标的最简单方法:

After reading lots of sample code into this matter, I'm trying to figure out the simplest way to achieve the following:

我希望能够安排调用回我的Alarm BroadcastReceiver的Intent,这又会触发我的服务。但是,我想进行设置,使其每天两次调用Intent,并且仅在尚未设置警报的情况下安排警报(同样用于取消警报)。

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();


推荐答案

在我看来,这很好,但是没有创建两个日历对象,您可以将时间间隔设置为

That seems alright to me, however instead of creating two calendar objects you could just set your interval to

AlarmManager.INTERVAL_DAY/2

除非您的意图不同。

此外,

alarms.cancel(firstCallIntent);
alarms.cancel(secondCallIntent);

应该足以取消所有这些类型的警报,而无需:

should be enough to cancel all alarms of those types, no need for:

firstCallIntent.cancel();

编辑:设置2个日历对象

Setting 2 calendar objects

//midday
Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.HOUR_OF_DAY, 12);
cal1.set(Calendar.MINUTE, 00);
cal1.set(Calendar.SECOND, 00);

//7pm
Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.HOUR_OF_DAY, 19);
cal2.set(Calendar.MINUTE, 00);
cal2.set(Calendar.SECOND, 00);

Calendar.getInstance()将返回日历对象并将其设置为当前系统时间。每个.set方法都会更改该日历对象的某个变量。因此,当前如果是晚上8点,它将在当天将闹钟设置为12点和7点,这将毫无用处。因此,如果要在第二天进行设置,则需要使用cal1.add(Calendar.DAY_OF_MONTH,01);添加额外的一天,第二天为该时间设置。希望这会有所帮助。

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.

这篇关于Android AlarmManager-安排定期触发每天两次触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆