My project is a radio app and I want to add a sleep timer so that the user can set it to close the app after a specified time.
3 Answers
Please use startTimer(long ms) to start countdown timer and cancel timer to stop. And use wakelock to continue timer after screen off.
CountDownTimer cTimer = null;
void startTimer(long time) {
if(!wakeLock.isHeld()) {
PowerManager mgr = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SonaHeartLock");
wakeLock.acquire();
}
cTimer = new CountDownTimer(time, 1000) {
public void onTick(long millisUntilFinished) {
timerSync = millisUntilFinished;
}
public void onFinish() {
timerSync = 0;
System.exit(0);
}
};
cTimer.start();
}
//cancel timer
void cancelTimer() {
if (cTimer != null) {
timerSync = 0;
cTimer.cancel();
cTimer=null;
if(wakeLock!=null && wakeLock.isHeld())
wakeLock.release();
}
}

- 47
- 8
-
And how it will apper in the app ui. I mean what about the xml – Mohamed Ibrahim May 23 '19 at 22:21
-
Have you never seen any music player app? use seekbar to select time and button to start timer. – Mayank Choudhary May 24 '19 at 08:32
-
i did not use that code before, please if you can give me more help to add the sleep timer in my android app. – Mohamed Ibrahim May 24 '19 at 15:27
-
Okay,you need these things : A Popup Window, Seekbar , Button. try to create these things first from external source. After this ask here for further help. – Mayank Choudhary May 25 '19 at 08:30
-
done i created a popup class with xml and i put on it 2 buttons (set timer/cancel timer) and seek bar. how can i progarm it to work with the timer code – Mohamed Ibrahim May 25 '19 at 20:05
-
also, I put a button in the main activity to open the popup window. – Mohamed Ibrahim May 25 '19 at 20:21
-
so now i can open the popup window and i see the seek bar with 2 buttons https://imgur.com/a/2vIDiva – Mohamed Ibrahim May 25 '19 at 20:33
-
Good job! But you should use Discrete seekbar for sleepTimer to select values of different interval and also show selected time Using TextView above seekbar. Then copy paste above code in service class or in activity than start timer by calling startTimer() from start button click listener and stop timer from stop button click listener. – Mayank Choudhary May 27 '19 at 07:46
Thread.sleep(2000);
will pause for 2 seconds (i.e 2000 ms). In an Android app I wrote several years ago I actually had:
Thread.currentThread().sleep(2000);
but I'm pretty sure the .currentThread()
was not needed. I was just learning Java at the time.
See Oracle's doc.
In more detail, what I actually had was a separate task:
class TimerUpdater extends AsyncTask<String, String, Void>{
}
That periodically checked the time (pausing using Thread.sleep()
) and displayed a countdown, and at zero set a flag that the main task periodically checked and ended the game when it saw it was set.
My app was terrible code, but I was learning both Java and Android at the same time as fast as I could, to make an app to help my kid in math class.
Android has changed a lot since then, though.

- 2,026
- 1
- 23
- 32
You can get the time from user and after that you can create one background process which count time everytime and check with user time. If both will match then immediately close your app

- 692
- 1
- 5
- 18
-
Can you give more details the code or any reference because I am new. – Mohamed Ibrahim May 22 '19 at 03:41
-
https://stackoverflow.com/questions/4075180/application-idle-time/4075857#4075857 @MohamedIbrahim it will work and more understandable for you – Bhaven Shah Jul 25 '19 at 06:35