0

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.

UdayKiran Pulipati
  • 6,579
  • 7
  • 67
  • 92
i_max 77
  • 45
  • 2
  • 8

3 Answers3

1

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • i am using alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,SystemClock.elapsedRealtime() + 20000, 1000 * 60 * 60,receiverIntent); And yet getting unexpected result , i started at 1:29 PM then service start at first at 1:29 PM then 1:48 PM and 2:01 PM???? – i_max 77 May 14 '13 at 09:01
  • @i_max77: Sounds like you have multiple alarms scheduled. Read the last paragraph of my answer. – CommonsWare May 14 '13 at 10:58
1

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 );
tobias
  • 2,322
  • 3
  • 33
  • 53
  • still it not working i am using : alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 20000,1000 * 60 * 60,pendingIntent); – i_max 77 May 13 '13 at 07:12
  • try to use AlarmManager.RTC_WAKEUP – tobias May 14 '13 at 07:32
  • After using RTC_WAKE_UP at 1:29 PM ,i am write date time in file in service class and got this unexpected result : 1.At 1:29 PM 2.then 1:48 PM 3.And 2:01 PM i have set repeated alarm of one hour interval.But it start service firstly at 1:29 then 1:48 ??? – i_max 77 May 14 '13 at 08:58
  • What value has ALARM_TRIGGER_AT_TIME? – tobias May 15 '13 at 07:37
  • SystemClock.elapsedRealtime() + 20000 – i_max 77 May 15 '13 at 10:17
0

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)

Adam reuben
  • 45
  • 1
  • 4