简体   繁体   中英

How to use a countdown timer correctly in android

I have a return date(somewhere in near furture) stored within the application.

Now i need a find out current time of the android phone, then i can find out the amount of mins hours to countdown from. the below is what i have at the moment, when i create the below object i pass it PinnedCountdownTimer pct = new PinnedCountdownTimer((getTimeToReturn().getTime()-System.currentTimeMillis()),1000,countdownTv);

public class PinnedCountdownTimer extends CountDownTimer {

TextView tv;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Date resultdate ;
public PinnedCountdownTimer(long millisInFuture, long countDownInterval,TextView tv) {
    super(millisInFuture, countDownInterval);
    this.tv = tv;
}

@Override
public void onFinish() {
    tv.setText("get back to your car!");
}

@Override
public void onTick(long millisUntilFinished) {
    resultdate = new Date(millisUntilFinished);
    tv.setText("Left: " + sdf.format(resultdate) );
}

}

this does not work correctly.....do you guys have any idea?

According to the documentation , you need to call pct.start() from what I'm reading. Otherwise, I'm not an Android developer, but there's also the Timer class which is in regular Java, and looks to be available in Android as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM