0

Hello I would like to create a basic but fully working alarm clock application just to increase my android programming skills but it has too many bug I have to rewrite it from scratch.

A pending intent that fires when the alarm is triggered. When you set a second alarm that uses the same pending intent, it replaces the original alarm.

What I did is whenever user creates new alarm i add new alarm to alarm SQLite table and even if the alarm repeats every day in week I use the lastInsertId of that alarm as unique id for my pending intent and it means 7 pending intens has the same ids...

A trigger time. If the trigger time you specify is in the past, the alarm triggers immediately.

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.DAY_OF_WEEK,1);

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

Even if I hardcode the values it always triggers immediately, since the Monday has passed what do I need to do?

weston
  • 54,145
  • 21
  • 145
  • 203
uplnypan
  • 47
  • 4
  • Can you show the hardcoded version then? – weston Oct 11 '16 at 11:55
  • This might help you find the next Monday: http://stackoverflow.com/a/24177555/360211 – weston Oct 11 '16 at 11:57
  • thats the one in the question – uplnypan Oct 11 '16 at 11:57
  • but is it neccesary to find next monday? why the official docs doesnot contain that information i need to manualy find next day? – uplnypan Oct 11 '16 at 11:57
  • So it's not entirely hardcoded then, it's based on the current date and time. – weston Oct 11 '16 at 11:58
  • You must give it the date and time to fire first. If you do not give it the next Monday, and instead give it one in the past it will fire immediately and that is in the docs (as you know). – weston Oct 11 '16 at 11:59
  • so i need to calendar = nextDateOfPickedDayOfWeek(currentDayOfWeek); calendar.set(Calendar.HOUR_OF_DAY, hour); calendar.set(Calendar.MINUTE, minute); and then calendar.getTimeInMillis() ? – uplnypan Oct 11 '16 at 12:05
  • Maybe, but you probably only want the next date if the time is in the past. e.g. if I pick Monday 10am for alarm and it's currently Monday 9am, I would expect it to go off in 1hrs time, not 1w 1hr. – weston Oct 11 '16 at 12:07
  • so basicaly if the picked day of week is smaller than the todays day of week or if the picked day of week is the same as todays but the selected time hasnot passed yet right? is there any other restriction is should be thinking of ? BTW i still dont understand one thing the calendargetTimeinMillis returns what? the milisecond passed from last device reboot? or ? – uplnypan Oct 11 '16 at 12:12
  • That's the date/time you set the Calendar to from a fixed epoch https://developer.android.com/reference/java/util/Calendar.html#getTimeInMillis() – weston Oct 11 '16 at 12:14
  • So what about daylight savings time? Would you want to rely on this alarm clock to get you up for work all year round as it is? – weston Oct 11 '16 at 12:16
  • What about if I go on holiday and the timezone changes? What will happen, and what should happen? Unfortunately what you thought was a simple problem is not. But you could ignore these issues just for learning about alarmmanager purposes, just don't release it or use it without more thought and work in these areas. – weston Oct 11 '16 at 12:19
  • is it that hard to make a alarm clock ? im done ... – uplnypan Oct 11 '16 at 12:20
  • what i have after user submits the form , i have arraylist with numbers of days , i have hour and minute , now i have to use for loop to loop through the number of days he picked and set alarm manager but there are so many ifs if the time has passed if not if timezone changed omfg it cant be perfect. im so frustrated but basicaly i dont even care about the timezone i just want my alarm to trigger at given time. i cant even find fully working example to understand what should be written in it ... – uplnypan Oct 11 '16 at 12:23
  • Like I said, you can certainly ignore those issues, just don't rely on it year round. I'll give you an idea in an answer shortly. – weston Oct 11 '16 at 12:26
  • thanks weston i appreciate , i dont want to copy paste or so but my code currently contains several if, the ifrst if is if current ad of week is equal to picked one and the picked time is smaller than the current time than i just set hour and minute to calendar object another if is if days are same BUT the picked time is bigger than the current time then i need to find the next date of the picked day. and the last if is if current day week is bigger than the picked one and then i need to find again the next date of the day but what it does, it triggers imediately – uplnypan Oct 11 '16 at 12:28
  • Was just going to be an idea, not code. You should read this page on the docs and there is an example app you can download from there too: https://developer.android.com/training/scheduling/alarms.html – weston Oct 11 '16 at 12:31
  • You know you can edit the question and paste the code in. You don't need to describe it. – weston Oct 11 '16 at 12:32
  • will you help me with that idea please? – uplnypan Oct 11 '16 at 13:07

0 Answers0