0

How to set up a repeating alarm to fire immediately when a Button is pressed and repeat itself in every 4 hrs.?

alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),   
4*60*60*1000, alarmIntent);

Repeats itself but doesnt start immediately when a Button is pressed. it takes 5 mins to fire.

What am I doing wrong here?

I changed the code as below and the alarm is fired immediately

alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, AlarmManager.INTERVAL_FIFTEEN_MINUTES, 4*60*60*1000, alarmIntent);

but it doesn't repeat every 4 hrs. How can I make the alarm repeat every 4 hrs?

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Reva
  • 225
  • 2
  • 6
  • 14

2 Answers2

0

Use setRepeating instead of setInexactRepeating.

Use setInexactRepeating when you want you alarm to fire around the specified time but you don't really need it to trigger exactly at that time.

This method exist so that the Android Os can manage to fire all the alarms at the same time in order to save battery life.

Take a look at this question, it might help you.

Quoting the accepted answer:

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
Timothée Jeannin
  • 9,652
  • 2
  • 56
  • 65
  • Jeannin- thanks but with the latest versions, setRepeating is considered as setInexactRepeating only right?? – Reva May 19 '14 at 07:33
  • I'm not aware of that. Maybe we should check the android source code to see what's the exact behavior. – Timothée Jeannin May 19 '14 at 07:36
  • From Google's documentation, as of API 19, all repeating alarms are inexact. How can i make the alarm to fire immediately for the first time. – Reva May 19 '14 at 07:50
0

That's probably because you are using setInexactRepeating.

From google's documentation for setInexactRepeating:

  • Your alarm's first trigger will not be before the requested time, but it might not occur for almost a full interval after that time. In addition, while the overall period of the repeating alarm will be as requested, the time between any two successive firings of the alarm may vary. If your application demands very low jitter, use setRepeating(int, long, long, PendingIntent) instead.

Try:

alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime()+2*1000,   
4*60*60*1000, alarmIntent);

and check if it fires at least after 2 secs .. Not sure if it will solve your problem, but might lead you to a workaround.

A Nice Guy
  • 2,676
  • 4
  • 30
  • 54
  • @arnab- thanks, From Google's documentation, as of API 19, all repeating alarms are inexact. How can i make the alarm to fire immediately for the first time – Reva May 19 '14 at 07:51
  • alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, AlarmManager.INTERVAL_FIFTEEN_MINUTES,4*60*60*1000, alarmIntent); This fires the alarm immediately, but doesn't repeat after 4 hrs. Pls help! – Reva May 20 '14 at 09:07
  • As far as I know you cannot setExact-repeating. You could create an exact alarm though, and once that rings set another alarm for after 4 hours, and keep doing that – Zen May 20 '14 at 09:10