Hey there i have a countdown timer which i used to show the user how much time is left in my app. But now i want if timer is running for 1 min and user closes the app after 30 sec and in next 5 sec user again open the app then time should start to run from remaining 25 seconds
CountDownAdapter countDown = new CountDownAdapter(60*1000, 1000);
countDown.setSourceActivity(MainActivity.this);
countDown.start();
public void onCountDownTimer(long millisUntilFinished)
{
secondsLefts = millisUntilFinished;
long secondsLeft = millisUntilFinished / 1000;
long hours = secondsLeft / 3600;
long minutes = (secondsLeft % 3600) / 60;
long seconds = secondsLeft % 60;
timerText = hours + "h: "+minutes + "m: "+seconds + "s";
if(timerText.equals("0h: 0m: 1s"))
{
}
timer.setText(timerText);
}
The lifecycle of an app looks like this:
So try to store the current remaining time and the exact current timestamp when onPause() is called (the user switches to another app). And when the user opens the app again, you need to catch the onResume() function.
Read out the stored timestamp and compare it with the current time. The time difference can now be substracted from the remaining time that you also stored.
There might be a better solution but it would be the easiest way to store the timestamp and remaining time in an XML file.
Related
I was learning jetpack compose and I can't seem to get my head around how to create a simple countDownTimer. What I want to do is create a countDownTimer that accepts three inputs from the user (hours, minutes, and seconds), and then when I click on the start button I want to show the timer in a LazyColomun . Any link that you think might help me understand this is acceptable for me.
You can use these codes to create a Countdown Timer:
input: a date
val time = (timerDate.time).minus(Calendar.getInstance().timeInMillis)
var timer by remember { mutableStateOf(time) }
LaunchedEffect(key1 = timer) {
if (timer > 0) {
delay(1000L)
timer -= 1000L
}
}
val secMilSec: Long = 1000
val minMilSec = 60 * secMilSec
val hourMilSec = 60 * minMilSec
val dayMilSec = 24 * hourMilSec
val hours = (time % dayMilSec / hourMilSec).toInt()
val minutes = (time % dayMilSec % hourMilSec / minMilSec).toInt()
val seconds = (time % dayMilSec % hourMilSec % minMilSec / secMilSec).toInt()
Text(text = String.format(" %02d:%02d:%02d", hours, minutes, seconds))
General Understanding
The most basic form of a timer, counts in seconds or milliseconds. Even if you ask for hours, minutes and seconds from the user you should get that all converted to seconds/milliseconds and start the timer on that basis.
For Example
Hours = 3
Minutes = 15
Seconds = 20
all converted to seconds
Hours = 3(hr) = 10800 seconds
Minutes = 15(min) = 15 * 60 = 900 seconds
Seconds = 20(sec) = 20 seconds
so your timer should count down from hr + min + sec = 11720 seconds
You simply do a conversion from seconds/milliseconds to the format you want to display the elapsed time in whenever you want to display (your refresh rate)
External Article
The below article should do what you want.
Let’s Make a Countdown Timer app using Android Compose
I am trying to record some audio from microphone on android wear, 3 seconds long.
In this case we can't do that with Thread.sleep(3000); because during those seconds main thread works.
Also I was trying with manipulate with time and I got current time in seconds from Calendar example:
Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
seconds = seconds + 2;
if (seconds == 60 || seconds == 61)
{
seconds = seconds - 60;
}
int sada; //with initialization here is pretty much same
do
{
sada = 105; //without this line is pretty much same
sada = c.get(Calendar.SECOND);
Log.d("SEKUNDI", String.valueOf(sada));
}while(sada != seconds);
Anyone help?
I started recording in main thread, and I created new thread in background and sleep it for 3 seconds. In a postBack method I called the stopRecording() method. Important code is:
Thread.currentThread();
Thread.sleep(3000);
This can be applied to any other similar problem. Thanks for comments.
I want to make a countdown until a certain day but I don't know how to do it.
I want the countdown to count days, hours, minutes and seconds.
The final day will be set into the countdown with the format DAY/MONTH/YEAR. Ex: 11/9/15
Thank you and sorry for my English :P
EDIT:
What I want is the next:
You have a string that's a date (20/9/15). I want to make a countdown that counts DAYS, HOURS, MINUTES & SECONDS from today till the date. The countdown should be displayed on a textView
Thanks :D
Assuming you're using Java (you don't say), use the getTime method of the java.util.Date objects indicating now and then, get the difference between them to figure out the number of days, hours, minutes, etc... remaining.
public String timeRemaining(Date then) {
Date now = new Date();
long diff = then.getTime() - now.getTime();
String remaining = "";
if (diff >= 86400000) {
long days = diff / 86400000
remining += "" + days + (days > 1) ? "days" : "day";
diff -= days * 86400000;
}
//... similar math for hours, minutes
return remaining;
}
I'm using currentTimeMillis(); to get a start time, then later using it again to get an end time. I then use delete start from end and get a value which is the duration between the two. I am using SimpleDateFormat to make all these values pretty and readable. the only thing is when i'm using a low value like 10 seconds (or 300 etc) and not the full blown long number (i.e. 1335718053126) I appear be getting out 01:00:10 or 01:02:12 etc on all my outputs? in fact if I just ask SimpleDateFormat to output a hh:mm:ss value against a 0 value it reads 01:00:00.
any one know why this is?
Found this neat little code if anyone else needs a solution to time formatting.
Source link
public String getNiceTime(long time) {
String format = String.format("%%0%dd", 2);
String seconds = String.format(format, time % 60);
String minutes = String.format(format, (time % 3600) / 60);
String hours = String.format(format, time / 3600);
String outPutTime = hours + ":" + minutes + ":" + seconds;
return outPutTime;
}
mySimpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Explanation: you are probably living in a GMT+1 time zone and unless you specify a different one, your formatter will pick your current one, so it considers 0 hours as GMT and as you are in GMT+1, it outputs 1 hour
i want to run an countdown time , in which i want to show days,hours,sec and milisec remaining for a specific date. and will be be keep changing till the end of the specific date.
Hope you can understand.
Thanks in advance.
Well, I think the problem is, that you dont know, how to work with the time. Here i have a method I use to calculate the amount of time of some items which I parse out of a db.
The param is a double value, which has got the whole time in seconds. It returns a string with the time in days, hours, minutes and seconds as string.
public String timeCalculate(double ttime) {
long days, hours, minutes, seconds;
String daysT = "", restT = "";
days = (Math.round(ttime) / 86400);
hours = (Math.round(ttime) / 3600) - (days * 24);
minutes = (Math.round(ttime) / 60) - (days * 1440) - (hours * 60);
seconds = Math.round(ttime) % 60;
if(days==1) daysT = String.format("%d day ", days);
if(days>1) daysT = String.format("%d days ", days);
restT = String.format("%02d:%02d:%02d", hours, minutes, seconds);
return daysT + restT;
}
For the countdown itself...take the target timestamp minus the actual one and voila, you've got seconds left :) Put those seconds to this method and you've got the remaining time. Now you just need to do some UI things ;)
Oh, and for the usual Unix Timestamp you can use this little method:
public static long getTimestamp() {
return System.currentTimeMillis() / 1000;
}