Jim
Jim

Reputation: 19582

Android notification for future date

I was wondering is there a limit on how far in the future we can schedule a notification via AlarmManager?
I know this sounds weird but I was wondering if there is some kind of time limitation e.g. within 2 weeks

Upvotes: 2

Views: 673

Answers (3)

fractalwrench
fractalwrench

Reputation: 4076

There doesn't seem to be any limitation within the AlarmManager class on how far into the future an alarm can be set. When you call setExact() or setWindow(), the AlarmManager implementation doesn't appear to do any validation beyond checking that the alarm date is in the future, and simply calls an IAlarmManager service to set the alarm.

private void setImpl(int type, long triggerAtMillis, long windowMillis, long intervalMillis,
            PendingIntent operation, WorkSource workSource, AlarmClockInfo alarmClock) {
        if (triggerAtMillis < 0) {
            /* NOTYET
            if (mAlwaysExact) {
                // Fatal error for KLP+ apps to use negative trigger times
                throw new IllegalArgumentException("Invalid alarm trigger time "
                        + triggerAtMillis);
            }
            */
            triggerAtMillis = 0;
        }

        try {
            mService.set(type, triggerAtMillis, windowMillis, intervalMillis, operation,
                    workSource, alarmClock);
        } catch (RemoteException ex) {
        }
    }

You will, however, be limited by the 64 bits in a long, as only a finite range of Dates can be represented. This means if you plan to set an alarm beyond Sun Aug 17 07:12:55 UTC 292278994, you may run into problems.

Another thing to consider is setting the alarm using RTC or RTC_WAKE_UP, as this will affect whether the device is woken up by the alarm.

Upvotes: 2

Ed Holloway-George
Ed Holloway-George

Reputation: 5149

Technically I believe there is a limit in the set() method of AlarmManager but it is so large, you can consider it limitless.

Consider this example:

    long futureInMillis = ...;
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);

The limit here for futureInMillis is Long.MAX_VALUE which is (2^63)-1 or 9,223,372,036,854,775,807, but I am assuming you wont be needing to wait that long!

Upvotes: 1

Errol Green
Errol Green

Reputation: 1387

No there is no time limitation as far as I can tell reading the API document. You can set the exact date by setExact.

Alarm Clock API Reference

Upvotes: 2

Related Questions