All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yy.androidlib.util.DelayTask Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
package com.yy.androidlib.util;

import android.os.Handler;

public abstract class DelayTask {

    private Handler mHandler;
    private long mDuration;
    private boolean hasTask;
    private long lastTime;

    public DelayTask(Handler handler, long duration) {
        mHandler = handler;
        mDuration = duration;
    }

    public abstract void runTask();


    public void run() {
        if (!hasTask) {
            long now = System.currentTimeMillis();
            long duration = now - lastTime;
            if (duration < mDuration) {
                hasTask = true;
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        runTask();
                        hasTask = false;
                        lastTime = System.currentTimeMillis();
                    }
                }, mDuration - duration);
            } else {
                runTask();
                hasTask = false;
                lastTime = System.currentTimeMillis();
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy