简体   繁体   中英

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. 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:

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.

Use void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation) to schedule a repeating alarm.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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