1

I have an alarm that should run everyday at 12AM (it is not a repeating alarm, i am setting it each time i need cz in some cases i dnt want it to run)

Intent myIntent = new Intent(AlarmService.this, AlarmService.class);
pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.AM_PM, Calendar.AM);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.add(Calendar.DAY_OF_MONTH, 1);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

The weird thing is that if i change the date/time manually to 12Am of the next day the alarm is fired. But if i set time to 11:59Pm of today and wait till 12AM nothing happens and if the time/date was automatically set, it is also not firing at 12AM. Any idea why is that hapennning or how can i fix it? Thank you

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
user1433460
  • 27
  • 1
  • 9

1 Answers1

0

There is some thing wrong in your code look at this one i use it before it is perfect and it will improve your code.

mikesir87
  • 1,785
  • 1
  • 20
  • 25
Omarj
  • 1,151
  • 2
  • 16
  • 43
  • well in my case, am restarting the alarm (my code above) inside the service class itself. Could this be causing the misbehavior ? @Omar Johari – user1433460 Nov 12 '12 at 08:39
  • 1
    @user1433460 u can use timer – Omarj Nov 12 '12 at 08:46
  • look at this case it is relative to your problem or not ??? http://stackoverflow.com/questions/12807577/is-it-possible-to-have-a-alarmmanager-inside-a-service-class – Omarj Nov 12 '12 at 08:47
  • I believe alarmManager is more suitable for my case. I want the service to run everyday at 12am and do a download; if it fails i reset the alarm to call the service in 1 hr to try again; and if it passes, i set the alarm to 12 am the next day. so please help me find best solution and thanks a lot – user1433460 Nov 12 '12 at 09:12
  • 1
    there is to way choose the one u feel it is better to u : 1- http://android-er.blogspot.com/2010/10/simple-example-of-alarm-service-using.html 2-http://www.vogella.com/articles/AndroidPerformance/article.html – Omarj Nov 12 '12 at 09:20
  • sry i cannot accept this as answer cz its not, but ill will upvote your posts as they were helpful. my issue was that threads were behaving not as expected – user1433460 Nov 13 '12 at 14:31
  • so u still have a problem ?? – Omarj Nov 13 '12 at 14:33
  • no i fixed it...it was caused by a threading issue (async task didnt finish executing and i was trying to do smtn on the main UI assuming async task was done) – user1433460 Nov 14 '12 at 09:03
  • 1
    i'm happy for u, but like u see your solution and problem far a way from your question.So be be careful next time debug before u ask :) – Omarj Nov 14 '12 at 09:07