i_max 77
i_max 77

Reputation: 45

start service which repeat at top of every hour and fetch information at fixed time interval

i have setting repeated alarm manager to start service which fetch the location and send SMS but currently i am only writing time duration in file to check the alarm accuracy .

I find that the alarm manager not working fine , i set for one hours interval but it fired at 30 min. interval . I left it for a day and find that after 12'o clock the alarm accuracy is right. What happening ??

My Activity class which start alarm :

enter code here

         public static final long ALARM_TRIGGER_AT_TIME = SystemClock.elapsedRealtime() + 20000;
         public static final long ALARM_INTERVAL = 1000 * 60 * 60 ;

         AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

         Intent myIntent = new Intent(AutoMainActivity.this, TrackerService.class);

         pendingIntent = PendingIntent.getService(AutoMainActivity.this, 0, myIntent, 0);

         alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,ALARM_TRIGGER_AT_TIME, 1000 * 60 * 60,pendingIntent);    

AND my service class :

TraceService :

  public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);

    //writing in file to view time
    java.util.Date systemDates = Calendar.getInstance().getTime(); 
    simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
    currentTime = simpleDateFormat.format(systemDates);

            // create file and write current time
    generateNoteOnSD("ONSTART"+currentTime+"\n","Onstart.txt"); 

}

After executing this apps at 3:18 PM time .The file created by service show the alarm time . Check this :

alarm manager time 

ONSTART2013-05-13-15:18:26

ONSTART2013-05-13-15:21:58

ONSTART2013-05-13-15:54:21

ONSTART2013-05-13-16:18:25

ONSTART2013-05-13-17:18:26

ONSTART2013-05-13-17:49:21

ONSTART2013-05-13-18:18:25

ONSTART2013-05-13-19:18:28

ONSTART2013-05-13-20:10:51

ONSTART2013-05-13-20:18:29

ONSTART2013-05-13-20:48:49

ONSTART2013-05-13-21:18:30

ONSTART2013-05-13-21:58:58

ONSTART2013-05-13-22:18:38

ONSTART2013-05-13-22:56:00

ONSTART2013-05-13-23:18:43

ONSTART2013-05-13-23:48:49

ONSTART2013-05-14-00:18:44

ONSTART2013-05-14-01:18:45

ONSTART2013-05-14-02:18:45

ONSTART2013-05-14-03:18:45

ONSTART2013-05-14-04:18:45

ONSTART2013-05-14-05:18:44

ONSTART2013-05-14-06:18:44

ONSTART2013-05-14-07:18:44

You can check that when alarm manager start at 15:18 PM , it start again after 30 min. approx. But after 12'o clock it work fine !!! How to fix it. I need that alarm start every one hour not before that .

Upvotes: 2

Views: 5090

Answers (3)

Cain Olivares
Cain Olivares

Reputation: 1

Not sure why everyone complicates this issue so much. I'm seeing it everywhere. Answers and answers about calculating the next hour.

There's absolutely no need to calculate anything. The the HOUR from the Calender and use that as the interval. The HOUR is called everytime the hour changes. That's all there really is to it.

Some devices may need a little tweaking to make sure the HOUR being called is not the same one for consecutive times. This is due to some devices calling the HOUR sometimes when the minute changes.

Upvotes: 0

vasanth
vasanth

Reputation: 715

Try with AlarmManager.RTC_WAKEUP once instead of AlarmManager.ELAPSED_REALTIME_WAKEUP There is an example in this : Alarm Manager Example

Upvotes: 1

HalR
HalR

Reputation: 11073

I suspect that you have multiple alarms pending. I would recommend that you clear out all of your alarms before you set this one.

 public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);

    // Cancel alarms
    try {
        alarmManager.cancel(intent);
    } catch (Exception e) {
        Log.e(TAG, "AlarmManager update was not canceled. " + e.toString());
    }
 }

(how to cancel alarms is answered well in this answer)

Upvotes: 0

Related Questions