MonoDroid的 - 每日使用警报管理计划的服务? [英] MonoDroid - Daily scheduled service using alarm manager?

查看:157
本文介绍了MonoDroid的 - 每日使用警报管理计划的服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图安排服务在用户指定的时间每​​天运行。我使用的是timepicker给运行一天中什么时间该服务对用户的控制。

I am trying to schedule a service to run daily at a user specified time. I am using a timepicker to give the user control over what time of day the service is run.

每个时间用户改变一天中的时间的服务运行我更新报警管理

Each time the user changes the time of day for the service to run I am updating the alarm manager.

下面是我的code要做到这一点:

Here is my code to do this:

void RescheduleOpeningRatesRetriever (string strTime)
{
    var i = new Intent (this.ApplicationContext, typeof (OpenRatesService));            
    var src = PendingIntent.GetService (this.ApplicationContext, 0, i, PendingIntentFlags.UpdateCurrent);
    var am = (AlarmManager)this.ApplicationContext.GetSystemService (Context.AlarmService);

    var hour = TimePickerPreference.GetHour (strTime);
    var minute = TimePickerPreference.GetMinute (strTime);

    var cal = Java.Util.Calendar.GetInstance (Java.Util.TimeZone.Default);
    cal.Set (Java.Util.CalendarField.HourOfDay, hour);
    cal.Set (Java.Util.CalendarField.Minute, minute);

    am.SetRepeating (AlarmType.RtcWakeup, cal.TimeInMillis, AlarmManager.IntervalDay, src);
}

我不是100%肯定,如果code被安排为我从我的测试结果好坏参半的服务才能正常运行。

I am not 100% sure if the code is scheduling the service to run properly as I have had mixed results from my tests.

这是我现在遇到的问题是每个我重新安排到上述code运行服务,该服务将立即启动时....我不希望启动该服务,直到配置当天的时间。

The problem that I am experiencing now is each time I re-schedule the service to run with the above code, the service is started instantly.... I don't want to start the service until the configured time of day.

我如何安排和更新服务,在一天中的指定时间运行,而不在与警报管理调度它立即运行的服务?

How can I schedule and update the service to run at a specified time of day without running the service instantly upon scheduling it with the alarm manager?

推荐答案

如果要为其设置报警时间是过去,然后报警将熄灭瞬间。所以首先检查时间是否在过去,如果再设置它第二天。

If the time for which you are setting the alarm is in the past then the alarm will goes off instantly. so first check whether the time is in past, if it is then set it for next day.

像::

Calendar rightNow = Calendar.getInstance()

if( cal.after( rightNow )  )
{
      am.SetRepeating (AlarmType.RtcWakeup, cal.TimeInMillis, AlarmManager.IntervalDay, src);
}
else
{
      cal.roll(Java.Util.CalendarField.DAY_OF_MONTH, 1);
      SetRepeating (AlarmType.RtcWakeup, cal.TimeInMillis, AlarmManager.IntervalDay, src);
}

这篇关于MonoDroid的 - 每日使用警报管理计划的服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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