0

i want to make a simple alarm clock and i need to implement alarm manager into it, the user marks checkboxes which days he wants to repeat the alarm and he sets time and then i just get the numbers of days in week and do the following :

private AlarmManager alarmMgr;
private PendingIntent alarmIntent;

alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.DAY_OF_WEEK, day);


alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY * 7, alarmIntent);

the alarm fires right after setting it , i spent 3 weeks finding a solution how to make a simple working alarm but i cant figure it out. i heard that if the day you choose is today then you dont need to set the day of week to calendar object and if the day is not today then i have to find the date of the nearest incoming DAY chosen by user. right?

uplnypan
  • 47
  • 4
  • The 2nd argument for [`AlarmManager.setRepeating`](https://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int,%20long,%20long,%20android.app.PendingIntent)) is `long triggerAtMillis` which is used to set " time in milliseconds that the alarm should first go off". You're setting it to `calendar.getTimeInMillis()` which is now, so it goes off immediately. When do you want the alarm to first go off? – Mark Cramer Oct 11 '16 at 00:20
  • i want the alarm manager to trigger at given day of week and hour and minute so lets say todays number of day in week is 3 its tuesday here, and lets say the user sets that he want to get alarmed every monday at 8:00 so it means the number of day is 2 and hour is 8 and minute is 0 now how do i tell my alarm to not trigger imediately because the day of week has already passed since its 3 atm , how it understand that it should look for the nearest monday not the one that has already passed , could you please answer this question ? i would appreciate – uplnypan Oct 11 '16 at 00:30
  • Did you check out the example here: https://developer.android.com/training/scheduling/alarms.html? – Mark Cramer Oct 11 '16 at 00:40
  • yes thats the main resource i read things from, but still i dont get it , why does the alarm triggers imediately if i input a day or minute or hour that has already passed... ? – uplnypan Oct 11 '16 at 00:49
  • Can you share what values you have for `hour`, `minute` and `day`? Try hard-coding those to `calendar.set(Calendar.HOUR_OF_DAY, 9);` `calendar.set(Calendar.MINUTE, 0);` and `calendar.set(Calendar.DAY_OF_WEEK, 1);`. If that doesn't work, delete the `calendar.set(Calendar.DAY_OF_WEEK, 1);` line. If that doesn't work, copy/paste the code _exactly_ from resource above and see if that works. – Mark Cramer Oct 11 '16 at 01:08
  • the alarm triggers imdiatelz – uplnypan Oct 11 '16 at 11:01

0 Answers0