如何在android中停止CountDownTimer [英] How to stop CountDownTimer in android

查看:78
本文介绍了如何在android中停止CountDownTimer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何停止这个计时器,知道吗?

How to stop this timer , any idea ?

我想在每个查询中重置计时器,但它会继续.每个新查询都会添加新的计时器.如何解决这个问题?

I want to reset timer in every query but it continues. Every new query it adds new timer. How to solve this?

 new CountDownTimer(zaman, 1000) {                     //geriye sayma

            public void onTick(long millisUntilFinished) {

                NumberFormat f = new DecimalFormat("00");
                long hour = (millisUntilFinished / 3600000) % 24;
                long min = (millisUntilFinished / 60000) % 60;
                long sec = (millisUntilFinished / 1000) % 60;

                cMeter.setText(f.format(hour) + ":" + f.format(min) + ":" + f.format(sec));
            }

            public void onFinish() {
                cMeter.setText("00:00:00");
            }
        }.start();

推荐答案

您可以将其赋值给一个变量,然后对该变量调用cancel()

You can assign it to a variable and then call cancel() on the variable

CountDownTimer yourCountDownTimer = new CountDownTimer(zaman, 1000) {                    
    public void onTick(long millisUntilFinished) {}

    public void onFinish() {}

    }.start();

yourCountDownTimer.cancel();

或者您可以在计数器范围内调用 cancel()

or you can call cancel() inside of your counter scope

new CountDownTimer(zaman, 1000) {                    
    public void onTick(long millisUntilFinished) {
        cancel();
    }

    public void onFinish() {}

    }.start();

阅读更多:https://developer.android.com/reference/android/os/CountDownTimer.html

这篇关于如何在android中停止CountDownTimer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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