Is it possible to simulate an increase in the speed of passage of time in Android?

I searched for a while, but could not find the same question.

I am writing an application where I need to plan a lot of alarms per day, and then do different things during the day, and I would like to take a test, say 24 hours in 24 minutes or something else these lines.

Is it possible to simulate a faster passage of time, both in the emulator and on the device?

If so, it would be great for me to test these things without losing development time. Be that as it may, I write them the way I think it will work, do trial tests, and then run the program myself to make sure that the behavior is correct, but it greatly slows down my development time to wait a day or two of which see bad behavior, and then try to fix it and repeat it. Any help would be greatly appreciated!

+4
source share
1 answer

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); } } // (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 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); // 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 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.

0
source

All Articles