Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continuous function of CountDownTimer

I am making an app with a function to start a Timer. However I am facing a very serious problem.

Now suppose I have stared the timer of 10 minutes using CountDownTimer. After 10 minutes it should give me a beep. Now suppose I have pressed the home button thus making an application go on background. CountDownTimer property is that it continues to countDown, thus after 10 minutes I will get a beep.

However this happens if the screen time is not out. If the screen time is out and the screen goes blank/black then the timer still continues but at its finish, nothing in the application runs like app is on pause and once i click any button on mobile to make the mobile active, the application starts running in the background again..

I want to stop this and want the app to run even the screen time is out too.

How can this be possible.

like image 770
Muhammad Umar Avatar asked Aug 24 '26 09:08

Muhammad Umar


1 Answers

You will have to get a partial wakelock to keep the CPU running , so that your timer continues to run. Otherwise the device goes to sleep.

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
 PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Countdown");
 wl.acquire();

In finish you can do a release.

 wl.release();
like image 141
nandeesh Avatar answered Aug 26 '26 22:08

nandeesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!