Search code examples
androidalarmmanagerandroid-alarms

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 .


Solution

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