I have made a simple timer app which update a textview every second.
I want it to make a beep every 10th second for a total duration of 100 seconds.
Bascially this is how I have made it (very schematic):
static long second, ticker, uptimemilli;
onCreate() {
second = ticker = 0;
Handler mHandler = new Handler();
uptimemilli = SystemClock.uptimeMillis();
mHandler.postAtTime(mUpdateTimeTask, uptimemilli+1000*ticker++);
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
if (second == 100)
return;
UpdateMyTextView(second++);
if (second%10 == 0)
PlayBeep();
mHandler.postAtTime(mUpdateTimeTask, uptimemilli+1000*ticker++);
}
};
This works well when the phone display is on. The TextView is updated every second and it gives a beep every 10th second. However, I want the app to make the beep sound every 10th second even if I turn the display off. The problem is that when I do that (by short click on the power button), the timer loop seems to go in slow motion. I get a beep sound maybe every 20-25 seconds. So the postAtTime does not appear to work when the screen is turned off. Does anybody know why and how can I make postAtTime to trigger at the time I specify even when the screen is turned off?
The only reason you are even getting that is because something else is causing the device to power back on every 20-25 seconds. Shortly after the screen goes dark, the CPU powers down, and your application will no longer run.
postAtTime()is working perfectly well. The device is simply in sleep mode.Because the device is in sleep mode.
Stop the device from going into sleep mode, such as via
android:keepScreenOn.