直播電商原始碼,活動開始時間計時器

zhibo系統開發發表於2021-11-16

直播電商原始碼,活動開始時間計時器實現的相關程式碼

 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);
    }

以上就是直播電商原始碼,活動開始時間計時器實現的相關程式碼, 更多內容歡迎關注之後的文章


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2842418/,如需轉載,請註明出處,否則將追究法律責任。

相關文章