简体   繁体   English

如何在日历中的特定日期设置闹钟

[英]How to set alarm on specific day in calendar

I need to set alarm in my app to specific day that has been choosen from calendar ? 我需要在应用中将闹钟设置为从日历中选择的特定日期吗? As I have noticed Alarm doesn't have such method. 如我所知,Alarm没有这种方法。 I understnad that I can do this usign calendar and setting the time amount left for this date. 我知道我可以使用此日历,并设置该日期剩余的时间。 But for example if user changes date,what happens ? 但是例如,如果用户更改日期,会发生什么? Maybe this question is stupid,sorry, I am newbie. 抱歉,这个问题很愚蠢,我是新手。

By using the AlarmManager class, you should be able to do this: 通过使用AlarmManager类,您应该能够做到这一点:

http://developer.android.com/reference/android/app/AlarmManager.html http://developer.android.com/reference/android/app/AlarmManager.html

Use public void set (int type, long triggerAtTime, PendingIntent operation) to set the time to fire it. 使用public void set (int type, long triggerAtTime, PendingIntent operation)来设置触发它的时间。

Use void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation) to schedule a repeating alarm. 使用void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)安排重复警报。

Here's how you can do this with your specified (date, hoursOfDay, minute, seconds and even millis): 这是您如何使用指定的日期(小时,天,小时,分钟,秒甚至毫秒)执行此操作的方法:

   AlarmManager mAlarmManager = (AlarmMAnager)  Context.getSystemService(Context.ALARM_SERVICE);
   Calendar mCal = Calendar.getInstance();
   Calendar mCalSet = (Calendar) mCal.clone();

   mCalSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
   mCalSet.set(Calendar.MINUTE, minute);
   mCalSet.set(Calendar.SECOND, second);
   mCalSet.set(Calendar.MILLISECOND, millis);

   if(calSet.compareTo(calNow) <= 0){
    //Today Set time passed, count to tomorrow
    mCalSet.add(Calendar.DATE, 1);
   }

   mAlarmManager.set(AlarmManager.RTC_WAKEUP, mCalSet.getTimeInMillis(), yourOperation);

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

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