-2

I want alarm service to broadcast a intent every 1 hr.

I do it using RTC_WAKEUP as follows:

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, AlarmSheduleHelper
                .getImmediateNextHour().getTimeInMillis(),
                AlarmManager.INTERVAL_HOUR, pendingIntent);

Now i want to do it using ELAPSED_REALTIME_WAKEUP .How can i do it?

alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, ELAPSED_TIME_REAL_OF_STARTING_HR, AlarmManager.INTERVAL_HOUR, pendingIntent);

How do i get ELAPSED_TIME_REAL_OF_STARTING_HR ie elapsedRealtime correspoding to starting hr?

user93796
  • 18,749
  • 31
  • 94
  • 150

5 Answers5

2

there is no ELAPSED_TIME_REAL_OF_STARTING_HR flag in android system, and you have to specify the start time of alarm in place of this flag in alarmManager.setRepeating() method

Jayesh
  • 3,661
  • 10
  • 46
  • 76
1

code snippet from AOSP API demo:

        // We want the alarm to go off 1hr from now.
        long firstTime = SystemClock.elapsedRealtime();
        firstTime += 60 * 60 * 1000;

        // Schedule the alarm!
        AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                        firstTime, 60*60*1000, sender);

You may want to follow the link to view the complete example.

If you what the alarm to go off on the hour (1am, 2am, ...), specify the type as RTC_WAKEUP.

accuya
  • 1,412
  • 14
  • 14
  • I dont want the alarm to ring 30 senconds from now.I want it to ring every hr .EG 1 am,2am ,3am, 4am etc .How do i get firstTime correspondsing tp 1 then? – user93796 Mar 15 '14 at 07:47
  • Then you should use `AlarmManager.RTC_WAKEUP`. – accuya Mar 15 '14 at 08:20
  • :I am alredy using RTC_WAKEUP .But the problem with RTC_WAKEUP is that when the phone is in power saver mode/ low battery .RTC_WAKEUP doesnot work sometimes. And hence i posedted the question – user93796 Mar 16 '14 at 07:11
  • 1
    @user93796 if your problem was with power saver mode, why didn't you state that in your question? – panini Mar 16 '14 at 20:48
  • The question still ishow to set alarm starting at specific hr using alarm type AlarmManager.ELAPSED_REALTIME_WAKEUP – user93796 Mar 18 '14 at 17:10
0

This will wake up the device to fire the alarm in 1 hour, and every 1 hour after that:

alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, AlarmManager.INTERVAL_HOUR, AlarmManager.INTERVAL_HOUR, pendingIntent);
Naddy
  • 2,664
  • 6
  • 25
  • 38
  • .First param should be startTime and not interval.So this is wrong – user93796 Mar 16 '14 at 07:11
  • @user93796 This is from the documentation. See this [link](http://developer.android.com/training/scheduling/alarms.html). The exact words are `Here are some examples of using ELAPSED_REALTIME_WAKEUP. Wake up the device to fire the alarm in 30 minutes, and every 30 minutes after that: // Hopefully your alarm will have a lower frequency than this! alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, AlarmManager.INTERVAL_HALF_HOUR, AlarmManager.INTERVAL_HALF_HOUR, alarmIntent);` – Naddy Mar 21 '14 at 15:55
0

I think accuya's answer should work, I just improved it that the alarm will ring at the start of each hour.

First, determine the time interval we should skip until the first alarm:

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR_OF_DAY, 1);
calendar.clear(Calendar.MINUTE);
calendar.clear(Calendar.SECOND);
calendar.clear(Calendar.MILLISECOND);
long firstAlarmInterval = calendar.getTimeInMillis() - System.currentTimeMillis();

So, if the current time is 7:35, the firstAlarmInterval will represent 25 minutes needed until the first launch at 8:00.

long firstTime = SystemClock.elapsedRealtime() + firstAlarmInterval;

AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                    firstTime, 60*60*1000, sender);
-1

One hour realtime from now should be this:

System.currentTimeMillis() + (60 * 60 * 1000); // 60 minutes

panini
  • 2,026
  • 19
  • 21
  • I dont think so. currentTimeMillis() give millis since epoch and SystemClock.elapsedRealtime() Returns milliseconds since boot, including time spent in sleep. – user93796 Mar 10 '14 at 19:35
  • so swap currentTimeMillis() for SystemClock.elapsedRealtime()? – panini Mar 10 '14 at 19:41
  • if the first paramter is AlarmManager.ELAPSED_REALTIME_WAKEUP then elapsedRealtime()? should be used – user93796 Mar 11 '14 at 09:13
  • 1
    http://stackoverflow.com/questions/1082437/android-alarmmanager they got examples using calander as well as boot time and the milliseconds since 1970 epoch unix timestamp – SSpoke Mar 14 '14 at 15:38