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();
Related
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
}
}
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 am a new develop of Android. I want to create an Application about time. The question is "how to call method every 30 second?"
Example: Every 30 second the application will send some message.
I do not know how to do it firstly, I use this function
time = new CountDownTimer(10000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
if(isDistanceStable()){
Toast.makeText(ChangeStatus.this, "Your speed is normal.", 3).show();
}else{
Toast.makeText(ChangeStatus.this, "Your speed is abnormal.", 3).show();
callManualRed();
}
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
time.cancel();
//intervalCheckDistance();
}
}.start();
}
but how to call it every 30 second. Please give me an example or some solutions to solve it. Thank you very much and Sorry with my English
cdt = new CountDownTimer(30000, 30000) {
public void onTick(long millisUntilFinished) {
// Method
}
public void onFinish() {
cdt.start(); // Call Again After 30 seconds
}
}.start();
Remember to call cdt.cancel(); when you want to end the timer
i want to save the time when countDownTimer begin working, is there any method like onStart()?
timeToDirectAnswer = new CountDownTimer(25000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
}
};
The timer starts ticking the moment start() is called on it. You can get the current time wherever start() is called.
I have got an application where I need to show counter from 3 to 1 then quickly switch to another activity. Will TimerTask will be suitable for doing this? Can anybody show me an example of exactly how to do it?
CountDownTimer Worked. Code for showing timer for 3 seconds is.
new CountDownTimer(4000, 1000) {
public void onTick(long millisUntilFinished) {
Animation myFadeOutAnimation = AnimationUtils.loadAnimation(countdown.this, R.anim.fadeout);
counter.startAnimation(myFadeOutAnimation);
counter.setText(Long.toString(millisUntilFinished / 1000));
}
public void onFinish() {
counter.setText("done!");
}
}.start();
I would better use a CountDownTimer.
If you want for example your counter to count 3 seconds:
//new Counter that counts 3000 ms with a tick each 1000 ms
CountDownTimer myCountDown = new CountDownTimer(3000, 1000) {
public void onTick(long millisUntilFinished) {
//update the UI with the new count
}
public void onFinish() {
//start the activity
}
};
//start the countDown
myCountDown.start();
Use CountDownTimer as shown below.
Step1: create CountDownTimer class
class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onFinish() {
dialog.dismiss();
// Use Intent to Navigate from this activity to another
}
#Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
}
}
Step2: create an object for that class
MyCount counter = new MyCount(2000, 1000); // set your seconds
counter.start();