Android timer计时器

桂和同
2023-12-01

 

 

 private void stopTime() {
        LogUtils.e("stopTime");
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (task != null) {
            task.cancel();
            task = null;
        }
    }


    Timer timer;
    TimerTask task;

    private void startTime(String time) {
        LogUtils.e("startTime");
        if (timer == null) {
            timer = new Timer();
        }
        if (task == null) {
            task = new TimerTask() {
                @Override
                public void run() {
                    startActivity(new Intent(RestService.this, RestHintActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                }
            };
        }

        time = time.replace("分钟", "");
//        long period = Integer.parseInt(time) * 60;
        long period = 10000;
        timer.schedule(task, period);
    }




timer回收
   timerTask.cancel();
                        timer.purge();将timer中的task为cancel的状态进行回收;
                        timerTask = null;
demo:

private void initTimer() {

        if (timer == null) {
            timer = new Timer();
        }
        if (timerTask == null) {
            timerTask = new TimerTask() {
                @Override
                public void run() {
                    timeIsRun = true;
                    if (video_duration > 999) {
                        return;
                    }
                    if (mediaPlayer != null) {
                        try {
                            video_position = mediaPlayer.getCurrentPosition() / 1000;

                        } catch (IllegalStateException e) {
                            Log.e(TAG, "timer中 media获取错误: ");
                        }
                    }
                    Log.e(TAG, "run video_position: "+video_position );
                    Log.e(TAG, "run video_duration: "+video_duration );
                    String current = BigDecimalUtils.div((video_duration - video_position) + "", video_duration + "", 5);

                    if (BigDecimalUtils.compare(current, video_first) && !video_is_first) {
//                        第一次超过1/4时,上报1/4视频进度;
                        Log.e(TAG, "上报 play_first: ");
                        commitAdEvent(adInfoUtils.getList_video_play_first());
                        video_is_first = true;
                    }
                    if (BigDecimalUtils.compare(current, video_midpoint) && !video_is_midpoint) {
//                        第一次超过2/4时,上报2/4视频进度;
                        Log.e(TAG, "上报 video_play_midpoint: ");
                        commitAdEvent(adInfoUtils.getList_video_play_midpoint());
                        video_is_midpoint = true;
                    }
                    if (BigDecimalUtils.compare(current, video_third) && !video_ist_third) {
//                        第一次超过3/4时,上报3/4视频进度;
                        Log.e(TAG, "上报 video_play_third: ");
                        commitAdEvent(adInfoUtils.getList_video_play_third());
                        video_ist_third = true;
                    }

                    handler.sendEmptyMessage(updataTimeType);

                    if (!videoDialog.isShowing()||video_position == video_duration) {
                        timeIsRun = false;
                        timerTask.cancel();
                        timer.purge();
                        timerTask = null;
                        return;
                    }

                    Log.e("TAG", "run video_position: " + video_position);
                }
            };
        }
        if (timeIsRun) {
            return;
        }
        timer.schedule(timerTask, 0, 500);
    }

 

 类似资料: