Jeeten Parmar
Jeeten Parmar

Reputation: 5787

AlarmManager does not fire on time

I want AlarmManager to be fired at 10:30 AM daily based on user mobile device time. It fires at 10:30 AM for sure but the problem is that after 10:30 AM, It repeats without time like after at every half hour or after any unusual time interval.

How to prevent this problem ? I am calling this on Successfull Login and Registration ButtonCick() Event. I also want to stop this if user Logout.

My code is as below :

        Intent myIntent = new Intent(Register.this, AlarmReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(Register.this,
                0, myIntent, 0);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        Calendar firingCal = Calendar.getInstance();
        Calendar currentCal = Calendar.getInstance();

        firingCal.set(Calendar.HOUR_OF_DAY, 10);
        firingCal.set(Calendar.MINUTE, 30);
        firingCal.set(Calendar.SECOND, 0);

        long intendedTime = firingCal.getTimeInMillis();
        long currentTime = currentCal.getTimeInMillis();

        if (intendedTime >= currentTime) {

            WakeLocker.acquire(getApplicationContext());

            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, intendedTime,
                    AlarmManager.INTERVAL_DAY, pendingIntent);

            WakeLocker.release();

        } else {

            WakeLocker.acquire(getApplicationContext());

            firingCal.add(Calendar.DAY_OF_MONTH, 1);
            intendedTime = firingCal.getTimeInMillis();

            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, intendedTime,
                    AlarmManager.INTERVAL_DAY, pendingIntent);

            WakeLocker.release();
        }

Upvotes: 4

Views: 2568

Answers (1)

Pararth
Pararth

Reputation: 8134

Code seems fine. If the target version is 19,

Note:

as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.

Source: http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int,long,long,android.app.PendingIntent)

Note:

Beginning in API 19, the trigger time passed to this method is treated as inexact: the alarm will not be delivered before this time, but may be deferred and delivered some time later. The OS will use this policy in order to "batch" alarms together across the entire system, minimizing the number of times the device needs to "wake up" and minimizing battery use. In general, alarms scheduled in the near future will not be deferred as long as alarms scheduled far in the future.

With the new batching policy, delivery ordering guarantees are not as strong as they were previously. If the application sets multiple alarms, it is possible that these alarms' actual delivery ordering may not match the order of their requested delivery times. If your application has strong ordering requirements there are other APIs that you can use to get the necessary behavior; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent).

Applications whose targetSdkVersion is before API 19 will continue to get the previous alarm behavior: all of their scheduled alarms will be treated as exact.

Source: http://developer.android.com/reference/android/app/AlarmManager.html#set(int,long,android.app.PendingIntent)

Please check if that works for your requirement.

Upvotes: 3

Related Questions