如果发现是CountDownTimer完成? [英] Find if CountDownTimer is Finished?

查看:143
本文介绍了如果发现是CountDownTimer完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是与Android上的CountDownTimer玩弄,我来到一个排序困境。在我的code,我有以下几点:

I was playing around with the CountDownTimer on Android and I came into sort of a dilemma. In my code, I have the following:

public class mCountDownTimer extends CountDownTimer{
    protected boolean hasFinished = false;
    public mCountDownTimer(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        // TODO Auto-generated constructor stub
    }
    public void onFinish(){
        hasFinished = true;
    }
    @Override
    public void onTick(long millisUntilFinished) {
        // TODO Auto-generated method stub

    }
}

基本上我想看看我的CountDownTimer已经完成。但我想把它调用的功能,我有一些code云:

Basically I want to find out if my CountDownTimer has finished. But in the function I want to call it in, I have some code that goes:

public void function(){
public boolean finished = false;
    if(interrupted)
        countDownTimer.cancel();
    if(temporaryCountHolder == false){
        countDownTimer.start();
        interrupted = true;
    }
}

我怎么能告诉我的计时器是否已经完成?我想要实现的东西,说:

How can i tell whether or not my timer has finished? I want to implement something that says:

 if(countDownTimer.hasFinished == true){
        Time now = new Time(); //finds the current time
        now.setToNow(); 
        String lsNow = now.format("%m-%d-%Y %I:%M:%S");
        lsNow += " just Started\n";
        try {
            dumpToFile("StepsChanged", lsNow);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 }

但是,如果我把这个声明之后

But if I put the statement right after

if(temporaryCountHolder == false) 

语句,则if语句与hasFinished将始终评估是假的。我怎样才能得到它,这样当且仅当计时器已经完成,我可以记录时间?

statement, then the if statement with hasFinished will always evaluate to be false. How can I get it so that I can record the time if and only if the timer has finished?

推荐答案

根据您的意见,为什么你所得到的是假值的原因是因为你正在执行的语句计时器停止了。

As per your comments, the reason why you are getting the false value is because you are executing the statements before the timer has stopped.

您可以像下面,

@Override
public void onFinish(){
        hasFinished = true;
        Time now = new Time(); //finds the current time
        now.setToNow(); 
        String lsNow = now.format("%m-%d-%Y %I:%M:%S");
        lsNow += " just Started\n";
        try {
            dumpToFile("StepsChanged", lsNow);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

要简单地记录你可以将这些方法countdowntimer类的onFinish方法的时间。我不知道 dumpToFile ,如果它是另一个类的方法,你可以把它的静态方法,并使用它,甚至一些合适的替代方法。希望这有助于。

To simply record the time you can just move those methods to the onFinish method of countdowntimer class. I don't know about dumpToFile if it is a method of another class you can make it a static method and use it or even some suitable alternative methods. Hope this helps.

这篇关于如果发现是CountDownTimer完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆