Reputation: 1473
In my app I have a service. It is started when the main activity starts and does the following:
timer.scheduleAtFixedRate (
new TimerTask() {
public void run() {
appendToFile("diff: "+(System.currentTimeMillis()-lastTime));
lastTime = System.currentTimeMillis();
}
}, 0, 1);
Basically, it creates a Timer
which updates every millisecond. On each update the timer looks at when it was last called, and saves the time between timer calls to a file. As there is always overhead in the system I expect each line of the output to be more than 1ms, maybe around 10.
Here is a snapchat of the app when it is asleep.
diff: 6
diff: 8
diff: 5
diff: 81
diff: 12
diff: 12
diff: 54
diff: 60
diff: 5734
diff: 77
diff: 9
diff: 20
diff: 6
diff: 21
diff: 10
diff: 13
diff: 16895
diff: 13
diff: 82
diff: 269
diff: 35
diff: 25
diff: 24
diff: 20
diff: 30
diff: 26
diff: 11
diff: 14
diff: 49
diff: 57
diff: 9
My question is regarding the somewhat weird results I sometimes get. 16895 in the middle indicates that the service for some reason waited almost 17 seconds to run its task when the system was idle. However, directly afterwards it shows more normal results between 10 and 30 milliseconds. I know that this is during the time the system was sleeping, because the log has a lot more entries. The service was never destroyed, as I log all calls to onDestroy()
as well.
Is there an explanation why the service would suddenly pause? Is there a way that I can ensure that it does run all the time?
Upvotes: 0
Views: 43
Reputation: 36035
Like you said, the system was sleeping. A service is "destroyed" when the service is taken out of memory. A sleeping system is just a temporary pause since it'll resume as soon as the CPU wakes up.
You can wake the system up by using AlarmManager which will schedule an even to happen at a specific time. It'll wake the system up. You can also use a wake lock yourself or use a WakefulBroadcastReceiver to keep the CPU running.
Just note that keeping the CPU running is a big battery drain, so it's a power to used sparingly and only when you absolutely need it.
Upvotes: 2