美文网首页Android开发经验谈Android开发Android开发
Android时间工具类二(自定义CountDownTimer倒

Android时间工具类二(自定义CountDownTimer倒

作者: 偶尔饿 | 来源:发表于2018-06-04 14:53 被阅读65次

    说到倒计时,首先了解一下关系。方便你的理解

    一天=24小时 =24*60分钟=24*60*60秒=24*60*60*1000毫秒(86400000毫秒);

    1小时=60分钟=60*60秒=60*60*1000毫秒(3600000毫秒)

    1分钟 =60秒=60*1000毫秒(60000毫秒)

    1秒=1000毫秒

    自定义类CountDownTimerUtils

    public class CountDownTimerUtils extends CountDownTimer {

    private TimeBean timeBean;

        private CountDownTimerListener listeners;

        private final String strZero ="0";

        private final int judgementValue =10;

        private final int eightySixMillionAndFourHundredThousand =86400000;

        private final int threeMillionAndSixHundredThousand =3600000;

        private final int sixtyThousand =60000;

        private final int oneThousand =1000;

        private final int oneHundred =10;

        /**

        * @param millisInFuture    The number of millis in the future from the call

        *                          to {@link #start()} until the countdown is done and {@link #onFinish()}

    *                          is called.

        * @param countDownInterval The interval along the way to receive

        *                          {@link #onTick(long)} callbacks.

    */

        public CountDownTimerUtils(long millisInFuture, long countDownInterval) {

    super(millisInFuture, countDownInterval);

            timeBean =new TimeBean();

        }

    public void setCountDownTimerListeners(CountDownTimerListener listeners) {

    this.listeners = listeners;

            start();

        }

    @Override

        public void onTick(long millisUntilFinished) {

    // "/" 得整数是天数

            int day = (int) (millisUntilFinished /eightySixMillionAndFourHundredThousand);

            //对天取余数  然后"/" 之后是是小时数

            int time = (int) (millisUntilFinished %eightySixMillionAndFourHundredThousand /threeMillionAndSixHundredThousand);

            //对小时取余数  然后"/" 之后是分钟数

            int branch = (int) (millisUntilFinished %threeMillionAndSixHundredThousand /sixtyThousand);

            //对分钟取余数  然后"/" 之后是秒数

            int second = (int) (millisUntilFinished %sixtyThousand /oneThousand);

            //"/" 之后是毫秒数 (这个位置毫秒数 是取得2位数)

            int milliSecond = (int) (millisUntilFinished %oneThousand /oneHundred);

            //天

            String strDay = String.valueOf(day);

            if (day

    strDay =strZero + strDay;

            }

    //小时

            String strTime = String.valueOf(time);

            if (time

    strTime =strZero + strTime;

            }

    //分

            String strBranch = String.valueOf(branch);

            if (branch

    strBranch =strZero + strBranch;

            }

    //  秒

            String strSecond = String.valueOf(second);

            if (second

    strSecond =strZero + second;

            }

    //毫秒

            String strMilliSecond = String.valueOf(milliSecond);

            if (milliSecond

    strMilliSecond =strZero + strMilliSecond;

            }

    timeBean.setStrDay(strDay);

            timeBean.setStrTime(strTime);

            timeBean.setStrBranch(strBranch);

            timeBean.setStrSecond(strSecond);

            timeBean.setStrMilliSecond(strMilliSecond);

            if (listeners !=null) {

    listeners.onSuccess(timeBean);

            }

    }

    @Override

        public void onFinish() {

    if (listeners !=null) {

    listeners.onFinish();

            }

    }

    public void cancels() {

    this.cancel();

            if (listeners !=null) {

    listeners =null;

            }

    if (timeBean !=null) {

    timeBean =null;

            }

    }

    }

    关系类 TimeBean 用来存放时间

    public class TimeBean {

    private String strDay ="00";

        private String strTime ="00";

        private String strBranch ="00";

        private String strSecond ="00";

        private String strMilliSecond ="00";

        private final String symbol =":";

        public void setStrDay(String strDay) {

    this.strDay = strDay;

        }

    public void setStrTime(String strTime) {

    this.strTime = strTime;

        }

    public void setStrBranch(String strBranch) {

    this.strBranch = strBranch;

        }

    public void setStrSecond(String strSecond) {

    this.strSecond = strSecond;

        }

    public void setStrMilliSecond(String strMilliSecond) {

    this.strMilliSecond = strMilliSecond;

        }

    /**

    * 要么是strDay开始(天为单位) 要么是strMilliSecond结束(毫秒单位)  strDay 和  strMilliSecond 不会并存

    *

        * @return

        */

        public StringtoStringDayOrMilliSecond() {

    if (TextUtils.equals(strDay, "00")) {

    return strTime +symbol +strBranch +symbol +strSecond +symbol +strMilliSecond;

            }else {

    return strDay +symbol +strTime +symbol +strBranch +symbol +strSecond;

            }

    }

    /**

    * 要么是strTime开始(小时为单位) 要么是strMilliSecond结束(毫秒单位)  strTime 和  strMilliSecond 不会并存

    *

        * @return

        */

        public StringtoStringTimeOrMilliSecond() {

    if (TextUtils.equals(strTime, "00")) {

    return strBranch +symbol +strSecond +symbol +strMilliSecond;

            }else {

    return strTime +symbol +strBranch +symbol +strSecond +symbol +strMilliSecond;

            }

    }

    /**

    * 最大天 ------》最小毫秒  strDay 和  strMilliSecond 并存

    *

        * @return

        */

        public StringtoStringDayAndMilliSecond() {

    return strDay +symbol +strTime +symbol +strBranch +symbol +strSecond +symbol +strMilliSecond;

        }

    /**

    * 最大小时 ------》最小毫秒    strTime 和  strMilliSecond 并存

    *

        * @return

        */

        public StringtoStringTimeAndMilliSecond() {

    if (strDay.equals("00")) {

    return strTime +symbol +strBranch +symbol +strSecond +symbol +strMilliSecond;

            }else {

    return Integer.valueOf(strDay) *24 + Integer.valueOf(strTime) +symbol +strBranch +symbol +strSecond +symbol +strMilliSecond;

            }

    }

    }

    对应接口 CountDownTimerListener 回调倒计时

    public interface CountDownTimerListener {

    void onSuccess(TimeBean str);

        void onFinish();

    }

    调用方式 

    // 第一次参数  是表示需要倒计时的时间   毫秒单位。 第二个参数 好多毫秒进行一次。我这里是10.因为我这里的毫秒只展示2位数。这个自己来决定

    CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(1000000, 10); countDownTimerUtils.setCountDownTimerListeners(new CountDownTimerListener() { 

    @Override 

    public void onSuccess(TimeBean bean) {

    //可以用tostring 方法。 入果没有你需要的toString  方法。可以自行添加

    //timeBean.getStrDay();

    //timeBean.getStrTime();

    //timeBean.getStrBranch();

    //timeBean.getStrSecond();

    //timeBean.getStrMilliSecond();

     goodsdetailTvCountDownTime.setText(bean.toStringTimeAndMilliSecond());

     } 

    @Override public void onFinish() { 

    goodsdetailTvCountDownTime.setText("00:00:00:00");

     } });

    一定要记得在合适的时候调用。cancels.一般是在onDestroy方法处理。 

    如果有什么问题,请留言。我们共同学习。谢谢了。喜欢记得赞一个哦。

    相关文章

      网友评论

        本文标题:Android时间工具类二(自定义CountDownTimer倒

        本文链接:https://www.haomeiwen.com/subject/qttysftx.html