1

Hello im trying to make a simple fully working alarm clock but im stuck on making proper alarms... what i dont understand is this :

https://developer.android.com/training/scheduling/alarms.html

// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);

// setRepeating() lets you specify a precise custom interval--in this case,
// 20 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
        1000 * 60 * 20, alarmIntent);
  • we set current time in milis to the calendar so if we get time in milis it will fire right now what is the purpose of setting current time in milis and then assigning the hour and minute ?

  • set time in milis sets the ammount of miliseconds from the year 1970 until now ?

  • calendar get time in milis returns the amount of milis since the hour we set? or ? i dont understand these lines at all, why do we set current time in millis and then set hour to fire?

  • EDIT - if i set alarm to trigger at 10:00 and its 10:05 what should i do ? it fires imediately........

uplnypan
  • 47
  • 4

1 Answers1

0

we set current time in milis to the calendar so if we get time in milis it will fire right now what is the purpose of setting current time in milis and then assigning the hour and minute ?

(Edit: not strictly true. getInstance() should set the time to the current date and time automatically - setting the time in millis is probably irrelevant here) - The purpose is to set the current year / month / day at the same time. If you only set the hour and and minute it is not obvious on which day that will fire.

set time in milis sets the ammount of miliseconds from the year 1970 until now ?

The current number of milliseconds since January 1st 1970 in the UTC timezone.

calendar get time in milis returns the amount of milis since the hour we set? or ? i dont understand these lines at all, why do we set current time in millis and then set hour to fire?

getTimeInMillis() in this case will return the number of milliseconds since January 1st 1970 in the UTC timezone, up to 08:30 of the current day.

EDIT - if i set alarm to trigger at 10:00 and its 10:05 what should i do ? it fires imediately........

It is possible that the time has already passed. Bear in mind the time is in UTC! If you are in America or Asia that could be significantly different to your interpretation of "now".

Knossos
  • 15,802
  • 10
  • 54
  • 91
  • but is it okay that set repeating in its parameters has the ammount of ms since 1970 ? is is correct?it contains the ammount of ms until this very moment so it fires instantly , shouldnot it contain the ammount until now + the ammount untill the planned alarm fire? – uplnypan Oct 11 '16 at 13:39
  • `setRepeating`. You should read the documentation. The first long indicates when the alarm first fires. The second long indicates how often after that it will repeat the alarm. – Knossos Oct 11 '16 at 15:01
  • so basicaly i need to get the milis of the incoming picked day with this http://stackoverflow.com/questions/3463756/is-there-a-good-way-to-get-the-date-of-the-coming-wednesday and then i need to set the milis to the setRepeating but how do i get the milis from the output of the function in the link i pasted in this comment – uplnypan Oct 11 '16 at 15:42
  • You can use the Calendar given from that function: `nextDayOfWeek(Calendar.WEDNESDAY)`. Instead of this `calendar.getTimeInMillis()` you can use `nextDayOfWeek(Calendar.WEDNESDAY).getTimeInMills()` – Knossos Oct 11 '16 at 15:47
  • and basicaly in which cases i have to look up for incoming day of week ? only if the currentdayofweek > desired day of week ? or they are same BUT the time that user wanted to fire alarm has passed ? any other restriction ? – uplnypan Oct 11 '16 at 15:56
  • I am sorry, but it is very difficult to understand you. You need to set the time for the alarm to go off, and how often you want the alarm to go off after that. – Knossos Oct 12 '16 at 07:28