Is there a way to save the exact passed time (either milliseconds, seconds or minutes is fine) of a CountDownTimer?
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
I tried increasing a variable in onTick(), but the problem is, my timer is cancel and restartable over a Button, and onTick() gets called everytime I start the timer, no matter if a second passed or not. So if I click the Button multiple times in 1 second, onClick() gets called every single click, rather than once per second.
You can use System.currentTimeMillis(), is this what you are after?
public class MyCountDownTimer extends CountDownTimer {
long timeStart;
public MyCountDownTimer(long x, long y) {
super(x, y);
timeStart = System.currentTimeMillis();
}
#Override
public void onTick(long millisUntilFinished) {
long timeLapsed = System.currentTimeMillis() - timeStart;
/// your code
}
#Override
public void onFinish() {
/// your code
}
}
Related
I want to make a method (service, alarm, etc.) that can be calculated after x downtime user with the app
Which closes the current activity
and will send the initial activity (login)
Thank you very much
http://androidbite.blogspot.in/2012/11/android-count-down-timer-example.html
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
Refer this link and some examples on countdown timer if you want to use this.
I answer
code is
private long startTime=15*60*1000; // 15 MINS IDLE TIME
private final long interval = 1 * 1000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countDownTimer = new MyCountDownTimer(startTime, interval);
}
#Override
public void onUserInteraction(){
super.onUserInteraction();
//Reset the timer on user interaction...
countDownTimer.cancel();
countDownTimer.start();
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
#Override
public void onFinish() {
//DO WHATEVER YOU WANT HERE
// CIERRA LA APP MATANDO EL PROCESO Y VUELVE A ABRIRLO.
android.os.Process.killProcess(android.os.Process.myPid());
}
#Override
public void onTick(long millisUntilFinished) {
}
}
use
act.finishAffinity();
act.startActivity(new Intent(act, actMain.class));
I would like to create a countdown timer that starts at 10 but only takes 5 seconds to count down to 0.
I have this code below from Google Source Code that counts down from 10:
new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
timerText.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
timerText.setText("done!");
}
}.start();
You should actually try doing it yourself first, but:
new CountDownTimer(5000, 500) {
public void onTick(long millisUntilFinished) {
timerText.setText("Half-seconds remaining: " + millisUntilFinished / 500);
}
public void onFinish() {
timerText.setText("done!");
}
}.start();
The CountDownTimer has two parameters in its constructor, one for the length of the timer as a whole (called millisInFuture, the first parameter), and one for how often the onTick function is called (which is countDownInterval). Both of these parameters are of the long variable type.
Please see CountDownTimer in the Android API.
I have an Android service running in the background.
I want to be notified after a specific period of time (22 seconds), so I wrote:
private CountDownTimer mCountDownTimer = new CountDownTimer(22*1000,22*1000) {
public void onTick(long millisUntilFinished) {}
public void onFinish() {
doSomething();
}
};
I run this, and get the notification after 40 seconds, and even 50 seconds. Am I doing something wrong? How can this be done?
Actually you are putting interval time as wll 22000, what this you are doing wrong. Second parameter is the interval.So , Do this :
CountDownTimer alertTimer = new CountDownTimer(22*1000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
// Do here what you want
}
#Override
public void onFinish() {
}
}.start();
I'm trying to pass the countdowntimer a variable and use that variable as the amount of milliseconds to count down. If I simply enter the value the countdown works properly, but if I pass it a long variable it just runs the onFinish function.
Here's the actual code:
public CountDownTimer countDown = new CountDownTimer(respawnTime, 1000) {
#Override
public void onTick(long millisUntilFinished) {
timer =(Integer)(int) millisUntilFinished / 1000;
if(timer < 31)
timerText.setTextColor(Color.parseColor("#FF0000"));
timerText.setText(timer.toString());
}
#Override
public void onFinish() {
timerText.setTextColor(Color.parseColor("#00FF00"));
timerText.setText("UP");
}
};
At this point I have respawnTime set to equal 360000 hoping for a 360 second countdown, but like I said it just immediately runs the onFinish. Simply changing the first parameter to a literal instead of a variable fixes everything but I need to use a variable here. Thanks in advance for the help!
Change
#Override
public void onTick(long millisUntilFinished) {
to
#Override
public void onTick(long respawnTime) {
use the variable you are sending in the constructor in onTick().
Edit
Here is one of mine
private class MyCountDown extends CountDownTimer
{
long duration, interval;
public MyCountDown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
duration = millisInFuture;
interval = countDownInterval;
start();
}
#Override
public void onFinish() {
secs = 10;
Intent intent = new Intent(CalibrationTimeoutScreen.this, CalibrationTakeTempScreen.class);
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);
CalibrationTimeoutScreen.this.finish();
}
#Override
public void onTick(long duration) {
cd.setText(String.valueOf(secs));
secs = secs - 1;
}
}
You did not run start(). Simply add .start() after the last '}' or add a new line calling countDown.start().
I want a timer to pop up when I click a button that will count down from 3 seconds. And it does that fine but i want it to also show the milliseconds so when I click the button the text would go from 3.0 to 0.1. How would I add the milliseconds to the text view?
new CountDownTimer(1000, 3000) {
public void onTick(long millisUntilFinished) {
textViewTimer.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
textViewTimer.setVisibility(View.INVISIBLE);
textViewLevelGained.setVisibility(View.INVISIBLE);
}
}.start();
This is what I have
Other SO questions suggest CountDownTimer doesn't do sub 1-second granularity well. Look into a different class, like TimerTask.
Otherwise, the following would work.
new CountDownTimer(3000, 1) {
public void onTick(long millisUntilFinished) {
textViewTimer.setText("" + millisUntilFinished / 1000
+ "." + millisUntilFinished % 1000);
}
public void onFinish() {
textViewTimer.setVisibility(View.INVISIBLE);
textViewLevelGained.setVisibility(View.INVISIBLE);
}
}.start();