If you drop all time-dependent tasks into one class, you can inherit this into a test class, where all system times are multiplied by some constant, and all user times are divided by some constant. Instead of directly calling any time-dependent methods, you call it through this class.
class TimeHandler { protected Context context; public TimeHandler(Context context) { mContext = context; } public long toSystemMillis(long userMillis) { return userMillis; } public long toUserMillis(long systemMillis) { return systemMillis; } public long toSystemDelay(long userDelay) { return userDelay; } public long toUserDelay(long systemDelay) { return systemDelay; } public void setAlarmAt(long userMillis, PendingIntent operation) { long systemMillis = toSystemMillis(userMillis); AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, systemMillis, operation); } public void setAlarmAfter(long userDelay, PendingIntent operation) { long systemDelay = toSystemDelay(userDelay); AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, systemDelay, operation); } }
UPDATE
You can do RATE and START_TIME constants in instance fields. Perhaps even the arguments of the constructor.
Usage example:
TimeHandler th = new TimeHandler(); th.setAlarmAt(Date.UTC(2011,9,19,0,0,0), operation);
If you cannot control the actual alarm planning, I donβt think there is a way to squeeze the time. You will have to use these classes to schedule alarms when you read them from the database.
source share