基于Handler的Android定时器与倒计时器

源码地址:Github AndroidTimer

特性

支持操作 :

start
pause
resume
cancel

使用

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
dependencies {
    ...
    compile 'com.github.xesam:AndroidTimer:v0.1'
}
TODO

 add ticker sequence

使用

CountTimer

    new CountTimer(100) {

        @Override
        public void onTick(long millisFly) { // millisFly is the Elapsed time at *Running State*
            vCountSwitcher.setText((millisFly) + "");
            Log.d("onTick", millisFly + "");
        }
    };
CountDownTimer

    new CountDownTimer(10_000, 100) {

        @Override
        public void onTick(long millisUntilFinished) { // millisUntilFinished is the left time at *Running State*
            Log.d("onTick", millisFly + "");
        }

        @Override
        public void onCancel(long millisUntilFinished) {
        }

        @Override
        public void onPause(long millisUntilFinished) {
        }

        @Override
        public void onResume(long millisUntilFinished) {
        }

        @Override
        public void onFinish() {
        }
    };

使用一个 Handler 同时管理多个定时任务

创建多个任务:

 MultiCountTimer multiCountTimer = new MultiCountTimer(100);
        multiCountTimer.add(new CounterTimerTask(1) {
            @Override
            public void onTick(long millisFly) {
                vMulti1.setText("multi_1:" + millisFly);
            }
        }).registerTask(new CounterTimerTask(2, 100) {
            @Override
            public void onTick(long millisFly) {
                vMulti2.setText("multi_2:" + millisFly);
            }
        }).registerTask(new CounterTimerTask(3, 1000) {
            @Override
            public void onTick(long millisFly) {
                vMulti3.setText("multi_3:" + millisFly);
            }
        });

        multiCountTimer.startAll();

取消任务:

 multiCountTimer.cancel(2);
    或者
    multiCountTimer.cancelAll();

原文地址:安卓巴士http://www.apkbus.com/blog-91...
截图
图片描述


莫非有道
91 声望7 粉丝

莫道,莫非有道!