美文网首页
关于CountDownTimer使用,设置页面定时跳转回到主页面

关于CountDownTimer使用,设置页面定时跳转回到主页面

作者: 朝阳夕晖蜂蜜 | 来源:发表于2019-06-17 16:37 被阅读0次

    参考:https://blog.csdn.net/qw286502698/article/details/72884712

    1、CountDownTimer使用,设置页面定时跳转回到主页面

    CountDownTimer是Google提供的一个倒计时工具类,利用这个工具可以很方便的实现倒计时功能。
    我在这里提供了一个CountTimer类用于继承CountDownTimer

    public class CountTimer extends CountDownTimer {
        private Context context;
        private CurrentTimeListener mCurrentTimeListener;
        /**
         * @param millisInFuture    倒计时总时间(如60S,120s等)
         * @param countDownInterval 渐变时间(每次倒计1s)
         */
        public CountTimer(long millisInFuture, long countDownInterval, Context context) {
            super(millisInFuture, countDownInterval);
            this.context = context;
        }
    
        /**
         * 定时触发回调
         * @param millisUntilFinished
         */
        @Override
        public void onTick(long millisUntilFinished) {
            if(mCurrentTimeListener!=null){
                mCurrentTimeListener.currentTime(millisUntilFinished);
            }
        }
        /**
         * Callback fired when the time is up.
         */
        @Override
        public void onFinish() {
            context.startActivity(new Intent(context, MainActivity.class));
        }
    
        public interface CurrentTimeListener{
            void currentTime(long currtenTime);
        }
    
        public void addCurrentTimeListener(CurrentTimeListener currentTimeListener){
            this.mCurrentTimeListener = currentTimeListener;
        }
    }
    

    我们在使用的时候通过BaseActivity中设置定时时长,如果Activity是MainActivity或者指定某一个Acticity不进行定时跳转功能,就要在该Activity通过接口回调去cancle掉计时器功能

     countTimerView.addCurrentTimeListener(new CountTimer.CurrentTimeListener() {
                @Override
                public void currentTime(long currtenTime) {
                    if(countTimerView!=null){
                        countTimerView.cancel();
                        Log.e(TAG, "currentTime: "+currtenTime );
                    }
                }
            });
    
    public class BaseAcvitity extends AppCompatActivity {
        CountTimer countTimerView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_base_acvitity);
            //设置时间为10s
            countTimerView = new CountTimer(1000*10,1000,BaseAcvitity.this);
        }
    
        private void timeStart() {
            new Handler(getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    countTimerView.start();
                }
            });
        }
    
        @Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
            switch (ev.getAction()) {
                case MotionEvent.ACTION_UP:
                    countTimerView.start();
                    break;
                default:
                    countTimerView.cancel();
                    break;
            }
            return super.dispatchTouchEvent(ev);
    
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            timeStart();
        }
    
        @Override
        protected void onDestory() {
            super.onPause();
            if(countTimerView!=null){
                countTimerView.cancel();
            }
        }
    }
    

    2、CountDownTimer使用,倒计时功能

    CountDownTimer 一步实现最简单的倒计时控件,参考
    https://blog.csdn.net/u010126792/article/details/88318919

    3、TimerTask计数器,倒计时

    Google官方解释TimerTask
    A task that can be scheduled for one-time or repeated execution by a Timer.

    TimeTask计数器的简单使用

    private Timer mTimer = new Timer();
     long time = 0;
    //开始计时器
     mBtnStart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            if(mTimer!=null){
                                mTimer.schedule(mTimerTask,1000,1000);
                            }
                        }
                    }).start();
    
                }
            });
          //关闭计时器
            mBtnStop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mTimer!=null){
                        mTimer.cancel();
                    }
                }
            });
    
        TimerTask mTimerTask = new TimerTask() {
            @Override
            public void run() {
                time++;
                Message message = new Message();
                message.what = 1;
                message.obj = time;
                mHandler.sendMessage(message);
    
            }
        };
        Handler mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 1) {
                    mTvTime.setText(String.valueOf(msg.obj));
                }
            }
        };
    

    警告,在开启和结束计数器功能,开启和结束按钮不能多次点击否则会出现异常
    java.lang.IllegalStateException: Task already scheduled or cancelled

    相关文章

      网友评论

          本文标题:关于CountDownTimer使用,设置页面定时跳转回到主页面

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