如何在android中设置计时器?

如何在android中设置计时器?,android,timer,Android,Timer,有人能举一个简单的例子,每隔一秒钟左右更新一个文本字段吗 我想做一个飞球,需要每秒计算/更新球坐标,这就是为什么我需要某种计时器 我没有从中获得任何信息。您需要创建一个线程来处理更新循环,并使用它来更新文本区域。然而,棘手的部分是,只有主线程才能真正修改ui,因此更新循环线程需要向主线程发出更新信号。这是使用处理程序完成的 查看此链接: 单击标题为“带有第二个线程的ProgressDialog示例”的部分。这正是你需要做的一个例子,除了使用进度对话框而不是文本字段。这很简单! 创建新的计时器 T

有人能举一个简单的例子,每隔一秒钟左右更新一个文本字段吗

我想做一个飞球,需要每秒计算/更新球坐标,这就是为什么我需要某种计时器


我没有从中获得任何信息。

您需要创建一个线程来处理更新循环,并使用它来更新文本区域。然而,棘手的部分是,只有主线程才能真正修改ui,因此更新循环线程需要向主线程发出更新信号。这是使用处理程序完成的

查看此链接: 单击标题为“带有第二个线程的ProgressDialog示例”的部分。这正是你需要做的一个例子,除了使用进度对话框而不是文本字段。

这很简单! 创建新的计时器

Timer timer = new Timer();
然后扩展计时器任务

class UpdateBallTask extends TimerTask {
   Ball myBall;

   public void run() {
       //calculate the new position of myBall
   }
}
然后以一定的更新间隔将新任务添加到计时器中

final int FPS = 40;
TimerTask updateBall = new UpdateBallTask();
timer.scheduleAtFixedRate(updateBall, 0, 1000/FPS);

免责声明:这不是理想的解决方案。这是使用Timer类的解决方案(如OP所要求的)。在Android SDK中,建议使用Handler类(在接受的答案中有示例)。

您希望在已经存在的UI线程中进行UI更新

最好的方法是使用一个处理程序,该处理程序使用postDelayed在延迟后运行Runnable(每次运行安排下一次运行);使用removeCallbacks清除回调


您已经找到了正确的位置,所以请再次查看,也许可以澄清为什么代码示例不是您想要的。(另请参见上的同一篇文章)。

好的,因为这个问题还没有解决,所以有3种简单的方法来处理这个问题。 下面是一个显示所有3个的示例,底部是一个显示我认为更可取的方法的示例。还记得在onPause中清理任务,必要时保存状态


import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Handler.Callback;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class main extends Activity {
    TextView text, text2, text3;
    long starttime = 0;
    //this  posts a message to the main thread from our timertask
    //and updates the textfield
   final Handler h = new Handler(new Callback() {

        @Override
        public boolean handleMessage(Message msg) {
           long millis = System.currentTimeMillis() - starttime;
           int seconds = (int) (millis / 1000);
           int minutes = seconds / 60;
           seconds     = seconds % 60;

           text.setText(String.format("%d:%02d", minutes, seconds));
            return false;
        }
    });
   //runs without timer be reposting self
   Handler h2 = new Handler();
   Runnable run = new Runnable() {

        @Override
        public void run() {
           long millis = System.currentTimeMillis() - starttime;
           int seconds = (int) (millis / 1000);
           int minutes = seconds / 60;
           seconds     = seconds % 60;

           text3.setText(String.format("%d:%02d", minutes, seconds));

           h2.postDelayed(this, 500);
        }
    };

   //tells handler to send a message
   class firstTask extends TimerTask {

        @Override
        public void run() {
            h.sendEmptyMessage(0);
        }
   };

   //tells activity to run on ui thread
   class secondTask extends TimerTask {

        @Override
        public void run() {
            main.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                   long millis = System.currentTimeMillis() - starttime;
                   int seconds = (int) (millis / 1000);
                   int minutes = seconds / 60;
                   seconds     = seconds % 60;

                   text2.setText(String.format("%d:%02d", minutes, seconds));
                }
            });
        }
   };


   Timer timer = new Timer();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        text = (TextView)findViewById(R.id.text);
        text2 = (TextView)findViewById(R.id.text2);
        text3 = (TextView)findViewById(R.id.text3);

        Button b = (Button)findViewById(R.id.button);
        b.setText("start");
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Button b = (Button)v;
                if(b.getText().equals("stop")){
                    timer.cancel();
                    timer.purge();
                    h2.removeCallbacks(run);
                    b.setText("start");
                }else{
                    starttime = System.currentTimeMillis();
                    timer = new Timer();
                    timer.schedule(new firstTask(), 0,500);
                    timer.schedule(new secondTask(),  0,500);
                    h2.postDelayed(run, 0);
                    b.setText("stop");
                }
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        timer.cancel();
        timer.purge();
        h2.removeCallbacks(run);
        Button b = (Button)findViewById(R.id.button);
        b.setText("start");
    }
}

需要记住的主要一点是,UI只能从主UI线程进行修改,因此请使用处理程序或活动

这是我认为最好的方法。


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TestActivity extends Activity {

    TextView timerTextView;
    long startTime = 0;

    //runs without a timer by reposting this handler at the end of the runnable
    Handler timerHandler = new Handler();
    Runnable timerRunnable = new Runnable() {

        @Override
        public void run() {
            long millis = System.currentTimeMillis() - startTime;
            int seconds = (int) (millis / 1000);
            int minutes = seconds / 60;
            seconds = seconds % 60;

            timerTextView.setText(String.format("%d:%02d", minutes, seconds));

            timerHandler.postDelayed(this, 500);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_activity);

        timerTextView = (TextView) findViewById(R.id.timerTextView);

        Button b = (Button) findViewById(R.id.button);
        b.setText("start");
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Button b = (Button) v;
                if (b.getText().equals("stop")) {
                    timerHandler.removeCallbacks(timerRunnable);
                    b.setText("start");
                } else {
                    startTime = System.currentTimeMillis();
                    timerHandler.postDelayed(timerRunnable, 0);
                    b.setText("stop");
                }
            }
        });
    }

  @Override
    public void onPause() {
        super.onPause();
        timerHandler.removeCallbacks(timerRunnable);
        Button b = (Button)findViewById(R.id.button);
        b.setText("start");
    }

}


如果您还需要在UI线程(而不是计时器线程)上运行代码,请查看博客:


如果有人感兴趣,我开始尝试创建一个标准对象来在活动UI线程上运行。看起来不错。欢迎评论。我希望它能作为一个组件在布局设计器上可用,并拖到活动上。真不敢相信这样的事情还不存在

package com.example.util.timer;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;

public class ActivityTimer {

    private Activity m_Activity;
    private boolean m_Enabled;
    private Timer m_Timer;
    private long m_Delay;
    private long m_Period;
    private ActivityTimerListener m_Listener;
    private ActivityTimer _self;
    private boolean m_FireOnce;

    public ActivityTimer() {
        m_Delay = 0;
        m_Period = 100;
        m_Listener = null;
        m_FireOnce = false;
        _self = this;
    }

    public boolean isEnabled() {
        return m_Enabled;
    }

    public void setEnabled(boolean enabled) {
        if (m_Enabled == enabled)
            return;

        // Disable any existing timer before we enable a new one
        Disable();

        if (enabled) {
            Enable();
        }
    }

    private void Enable() {
        if (m_Enabled)
            return;

        m_Enabled = true;

        m_Timer = new Timer();
        if (m_FireOnce) {
            m_Timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    OnTick();
                }
            }, m_Delay);
        } else {
            m_Timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    OnTick();
                }
            }, m_Delay, m_Period);
        }
    }

    private void Disable() {
        if (!m_Enabled)
            return;

        m_Enabled = false;

        if (m_Timer == null)
            return;

        m_Timer.cancel();
        m_Timer.purge();
        m_Timer = null;
    }

    private void OnTick() {
        if (m_Activity != null && m_Listener != null) {
            m_Activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    m_Listener.OnTimerTick(m_Activity, _self);
                }
            });
        }
        if (m_FireOnce)
            Disable();
    }

    public long getDelay() {
        return m_Delay;
    }

    public void setDelay(long delay) {
        m_Delay = delay;
    }

    public long getPeriod() {
        return m_Period;
    }

    public void setPeriod(long period) {
        if (m_Period == period)
            return;
        m_Period = period;
    }

    public Activity getActivity() {
        return m_Activity;
    }

    public void setActivity(Activity activity) {
        if (m_Activity == activity)
            return;
        m_Activity = activity;
    }

    public ActivityTimerListener getActionListener() {
        return m_Listener;
    }

    public void setActionListener(ActivityTimerListener listener) {
        m_Listener = listener;
    }

    public void start() {
        if (m_Enabled)
            return;
        Enable();
    }

    public boolean isFireOnlyOnce() {
        return m_FireOnce;
    }

    public void setFireOnlyOnce(boolean fireOnce) {
        m_FireOnce = fireOnce;
    }
}
在活动中,我有一个onStart:

@Override
protected void onStart() {
    super.onStart();

    m_Timer = new ActivityTimer();
    m_Timer.setFireOnlyOnce(true);
    m_Timer.setActivity(this);
    m_Timer.setActionListener(this);
    m_Timer.setDelay(3000);
    m_Timer.start();
}

他是更简单的解决方案,在我的应用程序中运行良好

  public class MyActivity extends Acitivity {

    TextView myTextView;
    boolean someCondition=true;

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.my_activity);

            myTextView = (TextView) findViewById(R.id.refreshing_field);

            //starting our task which update textview every 1000 ms
            new RefreshTask().execute();



        }

    //class which updates our textview every second

    class RefreshTask extends AsyncTask {

            @Override
            protected void onProgressUpdate(Object... values) {
                super.onProgressUpdate(values);
                String text = String.valueOf(System.currentTimeMillis());
                myTextView.setText(text);

            }

            @Override
            protected Object doInBackground(Object... params) {
                while(someCondition) {
                    try {
                        //sleep for 1s in background...
                        Thread.sleep(1000);
                        //and update textview in ui thread
                        publishProgress();
                    } catch (InterruptedException e) {
                        e.printStackTrace(); 

                };
                return null;
            }
        }
    }

这里有一个简单可靠的方法

将以下代码放入活动中,当活动处于“恢复”状态时,UI线程中每隔一秒就会调用tick()方法。当然,您可以更改tick()方法来执行您想要的操作,或者增加或减少调用的频率

@Override
public void onPause() {
    _handler = null;
    super.onPause();
}

private Handler _handler;

@Override
public void onResume() {
    super.onResume();
    _handler = new Handler();
    Runnable r = new Runnable() {
        public void run() {
            if (_handler == _h0) {
                tick();
                _handler.postDelayed(this, 1000);
            }
        }

        private final Handler _h0 = _handler;
    };
    r.run();
}

private void tick() {
    System.out.println("Tick " + System.currentTimeMillis());
}
对于那些感兴趣的人来说,“\u h0=\u handler”代码是必要的,以避免在勾选期间暂停并恢复活动时两个计时器同时运行

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity {

 CheckBox optSingleShot;
 Button btnStart, btnCancel;
 TextView textCounter;

 Timer timer;
 MyTimerTask myTimerTask;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  optSingleShot = (CheckBox)findViewById(R.id.singleshot);
  btnStart = (Button)findViewById(R.id.start);
  btnCancel = (Button)findViewById(R.id.cancel);
  textCounter = (TextView)findViewById(R.id.counter);

  btnStart.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {

    if(timer != null){
     timer.cancel();
    }

    //re-schedule timer here
    //otherwise, IllegalStateException of
    //"TimerTask is scheduled already" 
    //will be thrown
    timer = new Timer();
    myTimerTask = new MyTimerTask();

    if(optSingleShot.isChecked()){
     //singleshot delay 1000 ms
     timer.schedule(myTimerTask, 1000);
    }else{
     //delay 1000ms, repeat in 5000ms
     timer.schedule(myTimerTask, 1000, 5000);
    }
   }});

  btnCancel.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    if (timer!=null){
     timer.cancel();
     timer = null;
    }
   }
  });

 }

 class MyTimerTask extends TimerTask {

  @Override
  public void run() {
   Calendar calendar = Calendar.getInstance();
   SimpleDateFormat simpleDateFormat = 
     new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss a");
   final String strDate = simpleDateFormat.format(calendar.getTime());

   runOnUiThread(new Runnable(){

    @Override
    public void run() {
     textCounter.setText(strDate);
    }});
  }

 }

}
.xml



如果您只想安排一个倒计时,直到将来某个时间,并在这一过程中定期发出通知,那么您可以使用API级别1之后提供的类

new CountDownTimer(30000, 1000) {
    public void onTick(long millisUntilFinished) {
        editText.setText("Seconds remaining: " + millisUntilFinished / 1000);
    }

    public void onFinish() {
        editText.setText("Done");
    }
}.start();

也可以使用动画制作工具:

int secondsToRun = 999;

ValueAnimator timer = ValueAnimator.ofInt(secondsToRun);
timer.setDuration(secondsToRun * 1000).setInterpolator(new LinearInterpolator());
timer.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
    {
        @Override
        public void onAnimationUpdate(ValueAnimator animation)
        {
            int elapsedSeconds = (int) animation.getAnimatedValue();
            int minutes = elapsedSeconds / 60;
            int seconds = elapsedSeconds % 60;

            textView.setText(String.format("%d:%02d", minutes, seconds));
        }
    });
timer.start();

我认为你可以这样做:

 timerSubscribe = Observable.interval(1, TimeUnit.SECONDS)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Action1<Long>() {
                @Override
                public void call(Long aLong) {
                      //TODO do your stuff
                }
            });

Rx定时器这是定时器的一些简单代码:

Timer timer = new Timer();
TimerTask t = new TimerTask() {       
    @Override
    public void run() {

        System.out.println("1");
    }
};
timer.scheduleAtFixedRate(t,1000,1000);

对于那些不能依赖的人,我根据以下建议之一制作了一个实用课程:

public class TimerTextHelper implements Runnable {
   private final Handler handler = new Handler();
   private final TextView textView;
   private volatile long startTime;
   private volatile long elapsedTime;

   public TimerTextHelper(TextView textView) {
       this.textView = textView;
   }

   @Override
   public void run() {
       long millis = System.currentTimeMillis() - startTime;
       int seconds = (int) (millis / 1000);
       int minutes = seconds / 60;
       seconds = seconds % 60;

       textView.setText(String.format("%d:%02d", minutes, seconds));

       if (elapsedTime == -1) {
           handler.postDelayed(this, 500);
       }
   }

   public void start() {
       this.startTime = System.currentTimeMillis();
       this.elapsedTime = -1;
       handler.post(this);
   }

   public void stop() {
       this.elapsedTime = System.currentTimeMillis() - startTime;
       handler.removeCallbacks(this);
   }

   public long getElapsedTime() {
       return elapsedTime;
   }
 }
使用..只要做:

 TimerTextHelper timerTextHelper = new TimerTextHelper(textView);
 timerTextHelper.start();


如果你已经有时间了

public class Timer {
    private float lastFrameChanged;
    private float frameDuration;
    private Runnable r;

    public Timer(float frameDuration, Runnable r) {
        this.frameDuration = frameDuration;
        this.lastFrameChanged = 0;
        this.r = r;
    }

    public void update(float dt) {
        lastFrameChanged += dt;

        if (lastFrameChanged > frameDuration) {
            lastFrameChanged = 0;
            r.run();
        }
    }
}

因为这个问题仍然吸引着很多谷歌搜索的用户(关于安卓定时器),我想插入我的两枚硬币

首先,在Java9中该类将被弃用

建议的方法是使用更有效且功能丰富的方法,该方法可以额外安排命令在给定延迟后运行,或定期执行。此外,它还为ThreadPoolExecutor提供了额外的灵活性和功能

下面是一个使用普通功能的示例

  • 创建执行器服务:

    final ScheduledExecutorService SCHEDULER = Executors.newScheduledThreadPool(1);
    
  • 只需安排您的跑步时间:

    final Future<?> future = SCHEDULER.schedule(Runnable task, long delay,TimeUnit unit);
    
  • 希望您会发现这对在Android中创建任务非常有用

    完整示例:

    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    Future<?> sampleFutureTimer = scheduler.schedule(new Runnable(), 120, TimeUnit.SECONDS);
    if (sampleFutureTimer.isDone()){
        // Do something which will save world.
    }
    
    ScheduledExecutorService调度器=执行者。newScheduledThreadPool(1);
    Future sampleFutureTimer=scheduler.schedule(new Runnable(),120,TimeUnit.SECONDS);
    if(sampleFutureTimer.isDone()){
    //做些能拯救世界的事。
    }
    
    我很惊讶,没有答案会提到解决方案。它非常简单,并且提供了一种在Android中设置计时器的简单方法

    首先,如果尚未设置渐变依赖项,则需要设置渐变依赖项:

    implementation "io.reactivex.rxjava2:rxjava:2.x.y"
    
    (将
    x
    y
    替换为)

    因为我们只有一个简单的,非重复任务,所以我们可以使用
    可完成的
    对象:

    Completable.timer(2, TimeUnit.SECONDS, Schedulers.computation())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(() -> {
                // Timer finished, do something...
            });
    
    对于重复任务,您可以以类似方式使用可观察的:

    Observable.interval(2, TimeUnit.SECONDS, Schedulers.computation())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(tick -> {
                // called every 2 seconds, do something...
            }, throwable -> {
                // handle error
            });
    
    Schedulers.computation()
    确保计时器在后台线程上运行,而
    observeOn(AndroidSchedulers.mainThread())
    表示计时器完成后运行的代码将在主线程上完成


    为了避免不必要的内存泄漏,您应该确保在活动/片段被销毁时取消订阅。

    我将计时器提取出来,并将其作为一个单独的类:

    Timer.java

    并从
    计时器中提取主操作

    java

    我就是这样用的:

    MainActivity.java

    我希望这有助于我使用这种方式:

    String[] array={
           "man","for","think"
    }; int j;
    
    然后在onCreate下面

    TextView t = findViewById(R.id.textView);
    
        new CountDownTimer(5000,1000) {
    
            @Override
            public void onTick(long millisUntilFinished) {}
    
            @Override
            public void onFinish() {
                t.setText("I "+array[j] +" You");
                j++;
                if(j== array.length-1) j=0;
                start();
            }
        }.start();
    

    这是解决此问题的简单方法。

    对于想在kotlin这样做的人:

    val timer = fixedRateTimer(period = 1000L) {
                val currentTime: Date = Calendar.getInstance().time
                runOnUiThread {
                    tvFOO.text = currentTime.toString()
                }
            }
    
    要停止计时器,可以使用以下命令:

    timer.cancel()
    
    此函数还有许多其他选项,请尝试一下

    在此处输入代码
    
    enter code here
    Thread th=new Thread(new Runnable() {
            @Override
            public void run() {
                try { for(int i=0;i<5;i++) {
                    b1.setText(""+i);
                    Thread.sleep(5000);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
           pp();
           
                            }
                        }
                    });
                }} catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        th.start();
    
    Thread th=新线程(new Runnable(){ @凌驾 公开募捐{ tr
    Observable.interval(2, TimeUnit.SECONDS, Schedulers.computation())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(tick -> {
                // called every 2 seconds, do something...
            }, throwable -> {
                // handle error
            });
    
    import android.os.Handler;
    
    public class Timer {
    
        IAction action;
        Handler timerHandler = new Handler();
        int delayMS = 1000;
    
        public Timer(IAction action, int delayMS) {
            this.action = action;
            this.delayMS = delayMS;
        }
    
        public Timer(IAction action) {
            this(action, 1000);
        }
    
        public Timer() {
            this(null);
        }
    
        Runnable timerRunnable = new Runnable() {
    
            @Override
            public void run() {
                if (action != null)
                    action.Task();
                timerHandler.postDelayed(this, delayMS);
            }
        };
    
        public void start() {
            timerHandler.postDelayed(timerRunnable, 0);
        }
    
        public void stop() {
            timerHandler.removeCallbacks(timerRunnable);
        }
    }
    
    public interface IAction {
        void Task();
    }
    
    public class MainActivity extends Activity implements IAction{
    ...
    Timer timerClass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            ...
            timerClass = new Timer(this,1000);
            timerClass.start();
            ...
    }
    ...
    int i = 1;
    @Override
    public void Task() {
        runOnUiThread(new Runnable() {
    
            @Override
            public void run() {
                timer.setText(i + "");
                i++;
            }
        });
    }
    ...
    }
    
    String[] array={
           "man","for","think"
    }; int j;
    
    TextView t = findViewById(R.id.textView);
    
        new CountDownTimer(5000,1000) {
    
            @Override
            public void onTick(long millisUntilFinished) {}
    
            @Override
            public void onFinish() {
                t.setText("I "+array[j] +" You");
                j++;
                if(j== array.length-1) j=0;
                start();
            }
        }.start();
    
    val timer = fixedRateTimer(period = 1000L) {
                val currentTime: Date = Calendar.getInstance().time
                runOnUiThread {
                    tvFOO.text = currentTime.toString()
                }
            }
    
    timer.cancel()
    
    enter code here
    Thread th=new Thread(new Runnable() {
            @Override
            public void run() {
                try { for(int i=0;i<5;i++) {
                    b1.setText(""+i);
                    Thread.sleep(5000);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
           pp();
           
                            }
                        }
                    });
                }} catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        th.start();
    
    import android.annotation.TargetApi;
    import android.content.Context;
    import android.os.Build;
    import android.util.AttributeSet;
    import android.view.View;
    import android.widget.TextView;
    
    import java.util.Locale;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.util.concurrent.TimeUnit;
    
    public class TimerTextView extends TextView {
    
        private static final int DEFAULT_INTERVAL = 1000;
    
        private Timer timer = new Timer();
        private long endTime = 0;
        private long interval = DEFAULT_INTERVAL;
        private boolean isCanceled = false;
    
        public TimerTextView(Context context) {
            super(context);
        }
    
        public TimerTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public TimerTextView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        public TimerTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }
    
        @Override protected void onDetachedFromWindow() {
            super.onDetachedFromWindow();
            stopTimer();
        }
    
        @Override protected void onVisibilityChanged(View changedView, int visibility) {
            super.onVisibilityChanged(changedView, visibility);
            if (VISIBLE == visibility) {
                startTimer();
            } else {
                stopTimer();
            }
        }
    
        public void setInterval(long interval) {
            if (interval >= 0) {
                this.interval = interval;
                stopTimer();
                startTimer();
            }
        }
    
        public void setEndTime(long endTime) {
            if (endTime >= 0) {
                this.endTime = endTime;
                stopTimer();
                startTimer();
            }
        }
    
        private void startTimer() {
            if (endTime == 0) {
                return;
            }
            if (isCanceled) {
                timer = new Timer();
                isCanceled = false;
            }
            timer.scheduleAtFixedRate(new TimerTask() {
                @Override public void run() {
                    if (null == getHandler()) {
                        return;
                    }
                    getHandler().post(new Runnable() {
                        @Override public void run() {
                            setText(getDurationBreakdown(endTime - System.currentTimeMillis()));
                        }
                    });
                }
            }, 0, interval);
        }
    
        private void stopTimer() {
            timer.cancel();
            isCanceled = true;
        }
    
        private String getDurationBreakdown(long diff) {
            long millis = diff;
            if (millis < 0) {
                return "00:00:00";
            }
            long hours = TimeUnit.MILLISECONDS.toHours(millis);
            millis -= TimeUnit.HOURS.toMillis(hours);
            long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
            millis -= TimeUnit.MINUTES.toMillis(minutes);
            long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);
    
    
            return String.format(Locale.ENGLISH, "%02d:%02d:%02d", hours, minutes, seconds);
            //return "${getWithLeadZero(hours)}:${getWithLeadZero(minutes)}:${getWithLeadZero(seconds)}"
        }
    }
    
    import kotlin.concurrent.fixedRateTimer
    
    val timer = fixedRateTimer("Tag", false, 1000, 2500) { /* Your code here */ }