Equivalent to javax.swing.Timer in Android

前端 未结 3 938
无人共我
无人共我 2021-01-06 01:45

Is there something that looks like the javax.swing.Timer on Android. I know how to create own Threads, but is there something that is like the swing-timer?

3条回答
  •  走了就别回头了
    2021-01-06 02:26

    You are probably looking for the class android.os.CountDownTimer

    You can inherit the class like this:

    class MyTimer extends CountDownTimer
    {
        public MyTimer(int secsInFuture) {
            super(secsInFuture*1000, 1000); //interval/ticks each second.
        }
    
        @Override
        public void onFinish() {
            Log.d("mytag","timer finished!");
        }
    
        @Override
        public void onTick(long millisUntilFinished) {
            //fired on each interval
            Log.d("mytag","tick; " + millisUntilFinished + " ms left");
        }
    }
    

提交回复
热议问题