简体   繁体   中英

Is it possible to simulate an increased rate of passage of time in Android?

I searched for a while, but couldn't find the same question.

I am writing an application where I need to schedule a lot of alarms a day, and then do different things throughout the day, and I'd love to be able to test, say 24 hours in 24 minutes, or something along those lines.

Is it possible to simulate a faster passage of time, either in emulator, or on-device?

If it is, this would be great for me to be able to test these things without losing development time. As it is, I'm writing them in a way that I think will work, doing spot tests, and then running the program myself to verify that the behavior is correct, but it greatly slows my development time to have to wait a day or two between seeing a poor behavior, and then trying to fix it and retesting it. Any help would be greatly appreciated!

If you abstract all time-dependent tasks into a single class, you could inherit that into a test-class, where all system times are multiplied by some constant, and all user times are divided by some constant. Instead of calling any time-dependent methods directly, you call it trough 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);
    }
} // (scroll down)

class OptimizedTimeHandler extends TimeHandler {
    protected static final long RATE = 60; // 1 minute -> 1 hour
    protected static final long START_TIME = Date.UTC(2011,9,18,0,0,0);

    public TestTimeHandler(Context context) {
        super(context);
    }

    @Override
    public long toSystemMillis(long userMillis) {
        return START_TIME + (userMillis - START_TIME)/RATE;
    }
    @Override
    public long toUserMillis(long systemMillis) {
        return START_TIME + (userMillis - START_TIME)*RATE;
    }
    @Override
    public long toSystemDelay(long userDelay) {
        return userDelay/RATE;
    }
    @Override
    public long toUserDelay(long systemDelay) {
        return systemDelay*RATE;
    }
}

UPDATE

You could make the RATE and START_TIME constants into instance fields. Maybe even constructor arguments.

Example usage:

TimeHandler th = new TimeHandler();
th.setAlarmAt(Date.UTC(2011,9,19,0,0,0), operation);
// will fire at 2011-09-19 00:00:00 (real time)

th = new OptimizedTimeHandler();
th.setAlarmAt(Date.UTC(2011,9,19,0,0,0), operation);
// will fire at 2011-09-18 00:24:00 (real time)

If you can't control the actual scheduling of the alarms, I don't think there is any way to compress the time. You will have to use these classes to schedule the alarms when you are reading it from the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM