Android 实现 倒计时,定时任务,重启定时任务

最近做了一个Android项目,需要使用到链接,数据提交,等任务,需要使用到超时重链重发,一直问度娘都没找到好的方法,实现计时的功能。所以封装了一个简单的Util。

Android 实现 倒计时,定时任务,重启定时任务_第1张图片


DateUtil.java

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {

    /***
     * 根据时间字符串获取毫秒数
     */
    private static long getTimeMillis(String strTime) {
        long returnMillis = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date d = null;
        try {
            d = sdf.parse(strTime);
            returnMillis = d.getTime();
        } catch (ParseException e) {
        }
        return returnMillis;
    }

    /**
     * 根据毫秒数的差值来计算时间差 秒
     * */
    public static long getTimeSExpend(String startTime, String endTime){
        long longStart = getTimeMillis(startTime); //获取开始时间毫秒数
        long longEnd = getTimeMillis(endTime);  //获取结束时间毫秒数
        long longExpend = longEnd - longStart;  //获取时间差

        long longs = longExpend / (1000); //根据时间差来计算小时数
        return longs;
    }

    /**
     * 根据毫秒数的差值来计算时间差
     * 传入开始时间和结束时间字符串来计算消耗时长
     * */
    private String getTimeExpend(String startTime, String endTime){
        //传入字串类型 2016/06/28 08:30
        long longStart = getTimeMillis(startTime); //获取开始时间毫秒数
        long longEnd = getTimeMillis(endTime);  //获取结束时间毫秒数
        long longExpend = longEnd - longStart;  //获取时间差

        long longHours = longExpend / (60 * 60 * 1000); //根据时间差来计算小时数
        long longMinutes = (longExpend - longHours * (60 * 60 * 1000)) / (60 * 1000);   //根据时间差来计算分钟数

        return longHours + ":" + longMinutes;
    }

    /**
     * 传入结束时间和消耗时长来计算开始时间
     * */
    private String getTimeString(String endTime, String expendTime){
        //传入字串类型 end:2016/06/28 08:30 expend: 03:25
        long longEnd = getTimeMillis(endTime);
        String[] expendTimes = expendTime.split(":");   //截取出小时数和分钟数
        long longExpend = Long.parseLong(expendTimes[0]) * 60 * 60 * 1000 + Long.parseLong(expendTimes[1]) * 60 * 1000;
        SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        return sdfTime.format(new Date(longEnd - longExpend));
    }
}


TimerUtil.java

import android.annotation.SuppressLint;
import android.os.Handler;
import android.util.Log;

import com.rfid.ross.locationmachine.Interface.TimeResult;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2018-05-11.
 */
public class TimerUtil {

    private int ultimatelyTime = 10;

    private int linkTime = 1000;

    private String starTime = "";

    private TimeResult timeResult;

    private int code = 0;

    /**
     * 状态,0:未运行,1:正在运行
     * */
    private int state = 0;

    @SuppressLint("SimpleDateFormat")
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    /**
     * ultimatelyTime 最终时间  m/s
     * linkTime 每次运行调用间隔时间  m/s
     * */
    public TimerUtil(int ultimatelyTime, int linkTime,TimeResult timeResult,int code){
        this.ultimatelyTime = ultimatelyTime / 1000;
        this.linkTime = linkTime;
        this.timeResult = timeResult;
        this.code = code;
    }

    public void start(){
        this.state = 1;
        this.TimingHandler.removeCallbacks(this.TimingRunnable);
        this.TimingHandler.postDelayed(this.TimingRunnable, this.linkTime);
        this.starTime = this.sdf.format(new Date(System.currentTimeMillis()));
    }

    public void restart(){
        this.start();
    }

    public void destroy(){
        this.state = 0;
        TimingHandler.removeCallbacks(this.TimingRunnable);
    }

    //呼叫延时 1分钟
    private Handler TimingHandler = new Handler();
    private Runnable TimingRunnable = new Runnable() {
        @Override
        public void run() {
            if(DateUtil.getTimeSExpend(starTime,sdf.format(new Date(System.currentTimeMillis()))) > ultimatelyTime){
                //超时结束
                state = 0;
                TimingHandler.removeCallbacks(TimingRunnable);
                timeResult.OvertimeResult(code,"");
            }else{
                TimingHandler.postDelayed(this, linkTime);
                Log.i("TimeLog---",(ultimatelyTime - DateUtil.getTimeSExpend(starTime,sdf.format(new Date(System.currentTimeMillis()))))+"m/s");
                timeResult.CourseResult(code,"");
            }
        }
    };
}

TimeResult.java

/**
 * Created by Administrator on 2018-05-11.
 */
public interface TimeResult {

    /**
     * 过程返回
     * code 状态码
     * resule 消息
     * */
    void CourseResult(int code,String resule);

    /**
     * 结束返回
     * code 状态码
     * resule 消息
     * */
    void OvertimeResult(int code,String resule);
}

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Switch;

public class Main2Activity extends AppCompatActivity implements TimeResult{

    private int NO_OPRATION = 1;

    private int LINK_OVERTIME = 2;

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

        TimerUtil timerUtil = new TimerUtil(10 * 1000,1000,this,this.NO_OPRATION);
        timerUtil.start();
        TimerUtil timerUtil = new TimerUtil(10 * 1000,1000,this,this.LINK_OVERTIME);
        timerUtil.start();
    }

    @Override
    public void CourseResult(int code, String resule) {
        Log.d("CourseResult:","D");
    }

    @Override
    public void OvertimeResult(int code,String resule) {   
     Switch(code){
            case NO_OPRATION:
                //10s 无操作
                break;
            case LINK_OVERTIME:
                //10s 内链接超时,重新链接
                break;
        }

你可能感兴趣的:(Android)