2

I have four Repeating Alarms in my app each of which fires at different timings to do some important tasks. They are

  1. Alarm 1 - 12:30 AM
  2. Alarm 2 - 01:00 AM
  3. Alarm 3 - 06:00 AM
  4. Alarm 4 - 12:15 PM

Every alarm fires no problems. Time interval for each alarms are 24 hrs. But each of the firing at random timings.

Alarm Set Time Actual Firing Time Alarm 1 12:30 AM 06:31 AM Alarm 2 01:00 AM 06:01 AM Alarm 3 06:00 AM 06:01 AM Alarm 4 12:15 PM Still not fired 4 hours after that time

My Code was

Alarm 1

Intent myIntent=new Intent(MainPage.this,db_restore.class);

PendingIntent pi=PendingIntent.getBroadcast(MainPage.this,1,myIntent,0);

AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

Calendar calendar = Calendar.getInstance();

calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.AM_PM,Calendar.AM);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),1000*60*60*24,pi);

Alarm 2

Intent mor_flag=new Intent(MainPage.this,MorningFlag.class);

PendingIntent mor_intent=PendingIntent.getBroadcast(MainPage.this,3,mor_flag,0);

AlarmManager mor_alarm=(AlarmManager)getSystemService(Context.ALARM_SERVICE);

Calendar calendar=Calendar.getInstance();

calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 1);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.AM_PM,Calendar.AM);

mor_alarm.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),1000*60*60*24,mor_intent);

Alarm 3

Intent late_start1=new Intent(MainPage.this,late_start.class);

PendingIntent piLate=PendingIntent.getBroadcast(MainPage.this,2,late_start1,0);

AlarmManager alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);

Calendar calendar=Calendar.getInstance();

calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 6);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.AM_PM,Calendar.AM);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*60*24, piLate);

Toast.makeText(MainPage.this,"Alarm Service started for Late Start monitoring",Toast.LENGTH_LONG).show();

Alarm 4

Intent eve_flag=new Intent(MainPage.this,EveningFlag.class);

PendingIntent eve_intent=PendingIntent.getBroadcast(MainPage.this,4,eve_flag,0);

AlarmManager eve_alarm=(AlarmManager)getSystemService(Context.ALARM_SERVICE);

Calendar calendar=Calendar.getInstance();

calendar.add(Calendar.DATE, 1);
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 15);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.AM_PM, Calendar.PM);

eve_alarm.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),1000*60*60*24,eve_intent);

I don't know what I'm doing wrong. Please take a look and help me. Thanks.

Note: I'm checking this in API Level 19 Android 4.4.4 with minimum as 14.

Praveen Kumar
  • 547
  • 1
  • 7
  • 33

2 Answers2

1

Try using AlarmManager.setExact() and give some rep to this post

For api levels below 19 you should use AlarmManager.setRepeating() and your alarms will trigger exactly at specified time. Thou on api levels 19 and above this will no longer work. There was change in android so that all repeating alarms are inexact. So if you would like to achieve exact repeating alarm you should schedule alarm with AlarmManager.setExact() and then when alarm triggers do it again for next week and so on every week.

Community
  • 1
  • 1
Daniël van den Berg
  • 2,197
  • 1
  • 20
  • 45
  • If use `setExact()` can the devices less than API level 19 able to run this? – Praveen Kumar Oct 09 '15 at 11:22
  • @PraveenKumar I think so. To make sure you can always use an if statement checking the API level, see http://developer.android.com/training/basics/supporting-devices/platforms.html for more info. Depending on that you can choose which method to use. – Daniël van den Berg Oct 09 '15 at 11:27
1

From the API DOCS(https://developer.android.com/training/scheduling/alarms.html)

As of Android 4.4 (API Level 19), all repeating alarms are inexact. This means Android synchronizes repeating alarms from multiple apps and fires them at the same time.

So if you want an exact alarm use Alarm Manager's

setExact (int type, long triggerAtMillis, PendingIntent operation)

method. If you want repeating alarms with exact timing then you would have to write a logic where you will set an alarm (Not a repeating alarm) and when this alarm goes off set another alarm for next interval.

Dpk
  • 199
  • 1
  • 8
  • what if i need launch it each 30 minutes in a day? then i must implement 42 alarmmanagers? :? it is so overheady... – Dyno Cris Sep 15 '20 at 04:43