i_max 77
i_max 77

Reputation: 45

set repeated alarm manager for every hour in android

I want to fetch location every hours in android . For that i use alarm manager and set repeated alarm for every hour and just want to write into file after fix time i.e at 8 AM and 12 PM . I got a problem in setting alarm manager , while i set for every one hour but it execute in 1/2 hour .

on button click i start service :

       serviceButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

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

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

            AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
            alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, ALARM_TRIGGER_AT_TIME,
                    3600000, pendingIntent);

            //3600000 1hrs

            finish();
        }
    });

And Service Class are as :

Tracker Service.class

String FINAL_STRING;
SharedPreferences pref;
static final int START_TIME = 8;
static final int MID_TIME = 12;

    java.util.Date systemDates = Calendar.getInstance().getTime(); 
    int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);

    if(hour == START_TIME)
        {
            edit.putString("smsdata", FINAL_STRING);
            edit.commit();

            //sendSms(START_TAG+pref.getString("smsdata", ""));
            edit.putString("smsdata", "");
            edit.commit();
        }else {

            System.out.println("currentdate:"+simpleDateFormat.toString());
            System.out.println("current_time:"+currentTime);

            Editor edit = pref.edit();
            edit.putString("smsdata", pref.getString("smsdata", "")+FINAL_STRING+"#");
            edit.commit();

            if(hour==MID_TIME)
            {
                //sendSms(START_TAG+pref.getString("smsdata", ""));
                generateNoteOnSD("\n"+START_TAG+pref.getString("smsdata", ""));
                edit.putString("smsdata", "");
                edit.commit();
                System.out.println("mid time");

            }

        }

When i execute this the service start on every 30min. but i want on every 60min.

Upvotes: 0

Views: 10090

Answers (3)

Adam reuben
Adam reuben

Reputation: 55

In my application I have the functionality to trigger an alarm in 4 scenarios:

  1. Only once for a user-chosen date and time
  2. Daily for the chosen time
  3. Weekly according to chosen date and time
  4. User chosen custom days of the week I successfully implement the first 3 scenarios by using the following:

Only once:

Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.YEAR, Integer.parseInt(date[0]));
        calendar.set(Calendar.MONTH, (Integer.parseInt(date[1])) - 1);
        calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(date[2]));
        calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time[0]));
        calendar.set(Calendar.MINUTE, Integer.parseInt(time[1]));
        calendar.set(Calendar.SECOND, 0);

        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

For daily scheduling:

Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time[0]));
            calendar.set(Calendar.MINUTE, Integer.parseInt(time[1]));
            calendar.set(Calendar.SECOND, 0);

            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

For weekly scheduling (as per system date):

Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());

        calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
        calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time[0]));
        calendar.set(Calendar.MINUTE, Integer.parseInt(time[1]));
        calendar.set(Calendar.SECOND, 0);
        //long interval = calendar.getTimeInMillis() + 604800000L;
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent);

How do I implement weekly alarm scheduling for custom days of the week?

private void scheduleAlarm(int dayOfWeek) {

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);

    // Check we aren't setting it in the past which would trigger it to fire instantly
    if(calendar.getTimeInMillis() < System.currentTimeMillis()) {
        calendar.add(Calendar.DAY_OF_YEAR, 7);
    }

    // Set this to whatever you were planning to do at the given time
    PendingIntent yourIntent; 

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, yourIntent);
}

private void setUpAlarms() {

    scheduleAlarm(Calendar.MONDAY);
    scheduleAlarm(Calendar.FRIDAY);
}

Attribution Source: https://microeducate.tech/repeating-alarm-for-specific-days-of-week-android/ , Question Author : Dhruvil Patel(https://stackoverflow.com/users/1367157/dhruvil-patel) , Answer Author : MBH(https://stackoverflow.com/users/2296787/mbh)

Upvotes: 0

tobias
tobias

Reputation: 2362

setInexactRepeating() is the reason why it is not working as you expected. Try following:

AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, firstStart, interval, pendingIntent );

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006654

First, you really want to use one of the available constants, like INTERVAL_HOUR, with setInexactRepeating().

Second, setInexactRepeating() is inexact. Android reserves the right to flex the times of the alarms to coalesce events with other scheduled inexact alarms.

So, try switching briefly to setRepeating(). If it now works as you expect, your behavior is due to the "inexact" nature of setInexactRepeating().

Also, you can use adb shell dumpsys alarm to examine the scheduled alarms. It may be that you have two alarms scheduled, each going off once per hour.

Upvotes: 1

Related Questions