【问题标题】:How to set a recurring AlarmManager to execute code on a scheduled date?如何设置循环警报管理器以在预定日期执行代码?
【发布时间】:2013-04-08 09:49:05
【问题描述】:

我正在制作一个药物提醒应用程序,它会提醒人们在预定的日期时间服药。

例如,如果我的 开始日期是 2013 年 4 月 8 日,结束日期是 2013 年 4 月 12 日,时间是上午 11.30,那么警报应该在 2013 年 4 月 12 日上午 11.30 之前触发. 请帮助我如何实现这一目标。我知道如何设置警报管理器并触发正常警报。但是当我当时有开始日期和结束日期时,我对如何使用alarmManager.setRepeating() 方法感到困惑。

【问题讨论】:

  • 我认为你必须使用 'set()' 如果你想设置一个确切的日期而不是 'setRepeating()' developer.android.com/reference/android/app/…
  • 嘿,我会按照您建议的方式尝试代码。我希望我能得到我想要的东西..!!
  • 嘿感谢您的建议,它几乎完成了..!!

标签: android alarmmanager repeatingalarm


【解决方案1】:

此代码用于每天重复警报

Date dateobj=new Date(Year,month,day,hour,min);             
setRepeating(AlarmManager.RTC_WAKEUP, dateobj.getTime(), (24*60*60*1000), pendingIntent);

【讨论】:

  • 我想重复闹钟,直到我的结束日期到来。上面的代码可以满足我的要求吗?
  • 如果您只想安排四天 setrepeting 概念不起作用。请使用不同时间的不同请求代码
  • 那我该如何设置闹钟呢?
  • 日期 dateobj=new Date(Year,month,day,hour,min); // 按天递增 Intent intent1= new Intent(CurrentClass.this,service.class);//服务或广播pendingIntent =PendingIntent.getService(ScheduleManagement.this,1, intent1,3);//每次更改请求代码set(AlarmManager.RTC_WAKEUP, dateobj.getTime(), pendingIntent);
【解决方案2】:

您可以通过使用 setReapting() 方法在您想要的时间设置闹钟来做到这一点。

1.Use a table to insert your entries.
2.query the table for every minute or for the time interval you want using a broadcast receiver.
3.In the onReceive() callback compare the current time with the insert time and fire the alarm.
4.if there is no record then cancel the pending intent.

如果您对此有任何疑问,请通过此link

【讨论】:

    猜你喜欢
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多