android.os.CountDownTimer源码实例Demo

类android.os.CountDownTimer源码实例Demo

下面列出了android.os.CountDownTimer 类实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: input-samples   文件: WelcomeActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome_activity);
    final TextView countdownText = findViewById(R.id.countdownText);
    new CountDownTimer(5000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            int secondsRemaining = toIntExact(millisUntilFinished / 1000);
            countdownText.setText(getResources().getQuantityString
                    (R.plurals.welcome_page_countdown, secondsRemaining, secondsRemaining));
        }

        @Override
        public void onFinish() {
            if (!WelcomeActivity.this.isFinishing()) {
                finish();
            }
        }
    }.start();
}
 
public void setLoadingCircledImageView() {
    mCircledImageText.setText(R.string.loading);
    mCircledImage.setImageResource(R.drawable.stop);
    mCircledImage.setCircleBorderColor(mColorAccent);
    mCountDownTimer =
            new CountDownTimer(10000, 10) {
                @Override
                public void onTick(long millisUntilFinished) {
                    float timeElapsed = (10000.0f - millisUntilFinished) / 10000.0f;
                    mCircledImage.setProgress(timeElapsed);
                }

                @Override
                public void onFinish() {
                    setStartCircledImageView();
                }
            }.start();
}
 
/**
 * @param graphicOverlay Used to refresh camera overlay when the confirmation progress updates.
 */
ObjectConfirmationController(GraphicOverlay graphicOverlay) {
  long confirmationTimeMs = PreferenceUtils.getConfirmationTimeMs(graphicOverlay.getContext());
  countDownTimer =
      new CountDownTimer(confirmationTimeMs, /* countDownInterval= */ 20) {
        @Override
        public void onTick(long millisUntilFinished) {
          progress = (float) (confirmationTimeMs - millisUntilFinished) / confirmationTimeMs;
          graphicOverlay.invalidate();
        }

        @Override
        public void onFinish() {
          progress = 1;
        }
      };
}
 
源代码4 项目: imsdk-android   文件: FindPwdStep2Activity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.atom_ui_activity_find_pwd_step2);

    domainId = getIntent().getStringExtra(Constants.BundleKey.RESULT_DOMAIN_ID);
    QtNewActionBar actionBar = (QtNewActionBar) this.findViewById(R.id.my_action_bar);
    setNewActionBar(actionBar);
    setActionBarTitle(R.string.atom_ui_forget_pwd);

    timer = new CountDownTimer(millisInFuture,1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            atom_ui_get_sms_code.setText(countTimes-- + "s");
        }

        @Override
        public void onFinish() {
            atom_ui_get_sms_code.setText(R.string.atom_ui_get_verify_code);
            atom_ui_get_sms_code.setClickable(true);
        }
    };

    initView();
    showPicCode();
}
 
源代码5 项目: styT   文件: Reg.java
@Override
  public int onStartCommand(Intent intent, int flags, int startId) {

// 第一个参数是总时间, 第二个参数是间隔
      mCodeTimer = new CountDownTimer(129000, 1000) {
          @Override
          public void onTick(long millisUntilFinished) {
		// 广播剩余时间
              broadcastUpdate(IN_RUNNING, millisUntilFinished / 1000 + "");
          }

          @Override
          public void onFinish() {
		// 广播倒计时结束
              broadcastUpdate(END_RUNNING);
              // 停止服务
              stopSelf();
          }
      };
// 开始倒计时
      mCodeTimer.start();
      return super.onStartCommand(intent, flags, startId);
  }
 
源代码6 项目: styT   文件: msf_shell.java
@Override
  public int onStartCommand(Intent intent, int flags, int startId) {

// 第一个参数是总时间, 第二个参数是间隔
      mCodeTimer = new CountDownTimer(60000, 1000) {
          @Override
          public void onTick(long millisUntilFinished) {
		// 广播剩余时间
              broadcastUpdate(IN_RUNNING, millisUntilFinished / 1000 + "");
          }

          @Override
          public void onFinish() {
		// 广播倒计时结束
              broadcastUpdate(END_RUNNING);
              // 停止服务
              stopSelf();
          }
      };
// 开始倒计时
      mCodeTimer.start();
      return super.onStartCommand(intent, flags, startId);
  }
 
源代码7 项目: sealrtc-android   文件: DownTimer.java
/**
 * [倒计为time长的时间,时间间隔为mills]
 *
 * @param time
 * @param mills
 */
public void startDown(long time, long mills) {
    mCountDownTimer =
            new CountDownTimer(time, mills) {
                @Override
                public void onTick(long millisUntilFinished) {
                    if (listener != null) {
                        listener.onTick(millisUntilFinished);
                    } else {
                        Log.e(TAG, "DownTimerListener 监听不能为空");
                    }
                }

                @Override
                public void onFinish() {
                    if (listener != null) {
                        listener.onFinish();
                    } else {
                        Log.e(TAG, "DownTimerListener 监听不能为空");
                    }
                    if (mCountDownTimer != null) mCountDownTimer.cancel();
                }
            }.start();
}
 
private void startCountDownTime(long time) {
    timer = new CountDownTimer(time * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            //每隔countDownInterval秒会回调一次onTick()方法
            down_timer.setText(String.format(getString(R.string.count_down_str), millisUntilFinished / 1000));
        }

        @Override
        public void onFinish() {
            openDemo();
        }

    };
    timer.start();// 开始计时
}
 
private void startCountDownTime(long time) {
    timer = new CountDownTimer(time * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            //每隔countDownInterval秒会回调一次onTick()方法
            down_timer.setText(String.format(getString(R.string.count_down_str), millisUntilFinished / 1000));
        }

        @Override
        public void onFinish() {
            // TODO: 27/02/2017 广告演示:广告展示完毕需要调用该方法执行深度链接跳转
            closeActivity();
        }

    };
    timer.start();// 开始计时
}
 
源代码10 项目: KToast   文件: KToast.java
/**
 * Error type toast.
 * @param activity
 * @param message
 * @param gravity
 * @param duration
 */
public static void errorToast(final Activity activity, String message, final int gravity, int duration){
    final View view = (activity.getLayoutInflater().inflate(R.layout.layout_error_toast, null));

    ((TextView)view.findViewById(R.id.txtErrorToast)).setText(message);

    if (duration == LENGTH_AUTO){
        duration = Util.toastTime(message);
    }

    new CountDownTimer(Math.max(duration+1000, 1000), 2000){
        @Override
        public void onFinish() {

        }

        @Override
        public void onTick(long millisUntilFinished) {
            Toast toast = new Toast(activity);
            toast.setGravity(gravity, 0, 0);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setView(view);
            toast.show();
        }
    }.start();
}
 
源代码11 项目: KToast   文件: KToast.java
/**
 * Warning type toast.
 * @param activity
 * @param message
 * @param gravity
 * @param duration
 */
public static void warningToast(final Activity activity, String message, final int gravity, int duration){
    final View view = (activity.getLayoutInflater().inflate(R.layout.layout_warning_toast, null));

    ((TextView)view.findViewById(R.id.txtWarningToast)).setText(message);

    if (duration == LENGTH_AUTO){
        duration = Util.toastTime(message);
    }

    new CountDownTimer(Math.max(duration+1000, 1000), 2000){
        @Override
        public void onFinish() {

        }

        @Override
        public void onTick(long millisUntilFinished) {
            Toast toast = new Toast(activity);
            toast.setGravity(gravity, 0, 0);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setView(view);
            toast.show();
        }
    }.start();
}
 
源代码12 项目: KToast   文件: KToast.java
/**
 * Info type toast.
 * @param activity
 * @param message
 * @param gravity
 * @param duration
 */
public static void infoToast(final Activity activity, String message, final int gravity, int duration){
    final View view = (activity.getLayoutInflater().inflate(R.layout.layout_info_toast, null));

    ((TextView)view.findViewById(R.id.txtInfoToast)).setText(message);

    if (duration == LENGTH_AUTO){
        duration = Util.toastTime(message);
    }

    new CountDownTimer(Math.max(duration+1000, 1000), 2000){
        @Override
        public void onFinish() {

        }

        @Override
        public void onTick(long millisUntilFinished) {
            Toast toast = new Toast(activity);
            toast.setGravity(gravity, 0, 0);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setView(view);
            toast.show();
        }
    }.start();
}
 
源代码13 项目: KToast   文件: KToast.java
/**
 * Success type toast.
 * @param activity
 * @param message
 * @param gravity
 * @param duration
 */
public static void successToast(final Activity activity, String message, final int gravity, int duration){
    final View view = (activity.getLayoutInflater().inflate(R.layout.layout_success_toast, null));

    ((TextView)view.findViewById(R.id.txtSuccessToast)).setText(message);

    if (duration == LENGTH_AUTO){
        duration = Util.toastTime(message);
    }

    new CountDownTimer(Math.max(duration+1000, 1000), 2000){
        @Override
        public void onFinish() {

        }

        @Override
        public void onTick(long millisUntilFinished) {
            Toast toast = new Toast(activity);
            toast.setGravity(gravity, 0, 0);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setView(view);
            toast.show();
        }
    }.start();
}
 
源代码14 项目: KToast   文件: KToast.java
/**
 * Normal type toast.
 * @param activity
 * @param message
 * @param gravity
 * @param duration
 */
public static void normalToast(final Activity activity, String message, final int gravity, int duration){
    final View view = (activity.getLayoutInflater().inflate(R.layout.layout_normal_toast, null));

    ((TextView)view.findViewById(R.id.txtNormalToast)).setText(message);

    if (duration == LENGTH_AUTO){
        duration = Util.toastTime(message);
    }

    new CountDownTimer(Math.max(duration+1000, 1000), 2000){
        @Override
        public void onFinish() {

        }

        @Override
        public void onTick(long millisUntilFinished) {
            Toast toast = new Toast(activity);
            toast.setGravity(gravity, 0, 0);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setView(view);
            toast.show();
        }
    }.start();
}
 
源代码15 项目: stynico   文件: Reg.java
@Override
  public int onStartCommand(Intent intent, int flags, int startId) {

// 第一个参数是总时间, 第二个参数是间隔
      mCodeTimer = new CountDownTimer(129000, 1000) {
          @Override
          public void onTick(long millisUntilFinished) {
		// 广播剩余时间
              broadcastUpdate(IN_RUNNING, millisUntilFinished / 1000 + "");
          }

          @Override
          public void onFinish() {
		// 广播倒计时结束
              broadcastUpdate(END_RUNNING);
              // 停止服务
              stopSelf();
          }
      };
// 开始倒计时
      mCodeTimer.start();
      return super.onStartCommand(intent, flags, startId);
  }
 
源代码16 项目: stynico   文件: msf_shell.java
@Override
  public int onStartCommand(Intent intent, int flags, int startId) {

// 第一个参数是总时间, 第二个参数是间隔
      mCodeTimer = new CountDownTimer(60000, 1000) {
          @Override
          public void onTick(long millisUntilFinished) {
		// 广播剩余时间
              broadcastUpdate(IN_RUNNING, millisUntilFinished / 1000 + "");
          }

          @Override
          public void onFinish() {
		// 广播倒计时结束
              broadcastUpdate(END_RUNNING);
              // 停止服务
              stopSelf();
          }
      };
// 开始倒计时
      mCodeTimer.start();
      return super.onStartCommand(intent, flags, startId);
  }
 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome_activity);
    final TextView countdownText = findViewById(R.id.countdownText);
    new CountDownTimer(5000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            int secondsRemaining = toIntExact(millisUntilFinished / 1000);
            countdownText.setText(getResources().getQuantityString
                    (R.plurals.welcome_page_countdown, secondsRemaining, secondsRemaining));
        }

        @Override
        public void onFinish() {
            if (!WelcomeActivity.this.isFinishing()) {
                finish();
            }
        }
    }.start();
}
 
源代码18 项目: AndroidSDK   文件: ConfirmActivity.java
private void initUI(Confirm confirm) {
  if (confirm == null) {
    confirm = getIntent().getParcelableExtra(ARG_CONFIRM);
  }

  activityConfirmPhoneNumber.setText(confirm.getPhone());
  activityRepeatImage.setVisibility(View.GONE);
  activityConfirmTimer.setVisibility(View.VISIBLE);
  new CountDownTimer(confirm.getWait(), 1000) {
    public void onTick(long millisUntilFinished) {
      activityConfirmTimer.setText("0:" + String.valueOf(millisUntilFinished / 1000));
    }
    public void onFinish() {
      activityRepeatImage.setVisibility(View.VISIBLE);
      activityConfirmTimer.setVisibility(View.GONE);
    }
  }.start();
}
 
源代码19 项目: AIMSICDL   文件: Toaster.java
/**
 * Long toast message
 * TOAST_DURATION_MILLS controls the duration
 * currently set to 6 seconds
 * @param context Application Context
 * @param msg     Message to send
 */
public static void msgLong(final Context context, final String msg) {
    if (context != null && msg != null) {
        if (toast != null) {
            toast.cancel();
        }

        new Handler(context.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
                new CountDownTimer(Math.max(TOAST_DURATION_MILLS - SHORT_TOAST_DURATION, 1000), 1000) {
                    @Override
                    public void onFinish() {
                        toast.show();
                    }

                    @Override
                    public void onTick(long millisUntilFinished) {
                        toast.show();
                    }
                }.start();
            }
        });
    }
}
 
源代码20 项目: andOTP   文件: MainActivity.java
private boolean setCountDownTimerNow() {
    int secondsToBlackout = 1000 * settings.getAuthInactivityDelay();

    if (settings.getAuthMethod() == AuthMethod.NONE || !settings.getAuthInactivity() || secondsToBlackout == 0)
        return false;

    countDownTimer = new CountDownTimer(secondsToBlackout, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
        }

        @Override
        public void onFinish() {
            authenticate(R.string.auth_msg_authenticate);
            this.cancel();
        }
    };

    return true;
}
 
源代码21 项目: Huochexing12306   文件: PocketProtector.java
@Override
public boolean start(Object obj) {
	try{
		mSensorManger = (SensorManager)getmServiceContext().getSystemService(Context.SENSOR_SERVICE);
		mSensor = mSensorManger.getDefaultSensor(Sensor.TYPE_PROXIMITY);
		mSensorListener = new MySensorListener();
		request(AntiTheftService.REQUEST_SHOW_MSG, getName() + "防护已开启,将在10秒后正式生效" + SF.TIP);
		mCDTimer = new CountDownTimer(10000, 1000) {
			
			@Override
			public void onTick(long millisUntilFinished) {
			}
			
			@Override
			public void onFinish() {
				mSensorManger.registerListener(mSensorListener, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
			}
		};
		setNeedDelay(true);
		mCDTimer.start();
		return true;
	}catch(Exception e){
		request(AntiTheftService.REQUEST_SHOW_MSG, "开启\"" + getName() + "\"防护失败,可能您的设备不支持距离传感器!");
		return false;
	}
}
 
源代码22 项目: photo-editor-android   文件: PhotoEditorActivity.java
private void returnBackWithSavedImage() {
    updateView(View.GONE);
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
    parentImageRelativeLayout.setLayoutParams(layoutParams);
    new CountDownTimer(1000, 500) {
        public void onTick(long millisUntilFinished) {

        }

        public void onFinish() {
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String imageName = "IMG_" + timeStamp + ".jpg";
            Intent returnIntent = new Intent();
            returnIntent.putExtra("imagePath", photoEditorSDK.saveImage("PhotoEditorSDK", imageName));
            setResult(Activity.RESULT_OK, returnIntent);
            finish();
        }
    }.start();
}
 
源代码23 项目: fritz-examples   文件: BaseRecordingActivity.java
private void showStartRecordingViews() {
    videoRecordingProgress.setVisibility(View.VISIBLE);
    videoRecordingProgress.setMax(NUM_PROGRESS_INTERVALS);
    videoRecordingProgress.setProgress(0);

    chooseModelBtn.setVisibility(View.GONE);
    mCountDownTimer = new CountDownTimer(MAX_RECORDING_TIME_MS, TIME_BETWEEN_RECORDING_INTERVAL_MS) {

        @Override
        public void onTick(long millisUntilFinished) {
            int progress = (int) (NUM_PROGRESS_INTERVALS - millisUntilFinished / TIME_BETWEEN_RECORDING_INTERVAL_MS);
            videoRecordingProgress.setProgress(progress);
        }

        @Override
        public void onFinish() {
            // finish recording when MAX_RECORDING_TIME_MS is met
            if (isRecording.compareAndSet(true, false)) {
                videoRecordingProgress.setProgress(NUM_PROGRESS_INTERVALS);
                showFinishRecordingViews();
            }
        }
    };
    mCountDownTimer.start();
}
 
源代码24 项目: haven   文件: MonitorActivity.java
private void initTimer() {
    txtTimer.setTextColor(getResources().getColor(R.color.colorAccent));
    cTimer = new CountDownTimer((preferences.getTimerDelay()) * 1000, 1000) {

        public void onTick(long millisUntilFinished) {
            mOnTimerTicking = true;
            txtTimer.setText(getTimerText(millisUntilFinished));
        }

        public void onFinish() {

            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            txtTimer.setText(R.string.status_on);
            initMonitor();
            mOnTimerTicking = false;
        }

    };

    cTimer.start();


}
 
源代码25 项目: android-openGL-canvas   文件: DrawTextActivity.java
private void initTextureView() {
    drawTextTextureView = findViewById(R.id.media_player_texture_view);
    final TextView frameRateTxt = findViewById(R.id.frame_rate_txt);

    drawTextTextureView.setOnSurfaceTextureSet(new GLSurfaceTextureProducerView.OnSurfaceTextureSet() {
        @Override
        public void onSet(SurfaceTexture surfaceTexture, RawTexture surfaceTextureRelatedTexture) {
            // No need to request draw because it is continues GL View.

            mediaSurface = new Surface(surfaceTexture);
        }
    });
    countDownTimer = new CountDownTimer(1000 * 3600, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            frameRateTxt.setText(String.valueOf(drawTextTextureView.getFrameRate()));
        }

        @Override
        public void onFinish() {

        }
    };
    countDownTimer.start();
}
 
private void handleResult(JSONObject json) {
    detectedLayout.setVisibility(View.VISIBLE);
    sensingLayout.setVisibility(View.INVISIBLE);

    String recognizedGesture = json.optString("recognized");
    detectedGesture.setText(recognizedGesture);
    score.setText(String.format(getString(R.string.gesture_score),json.optString("score")));
    sayText(recognizedGesture);

    new CountDownTimer(1500, 1500) {
        public void onTick(long millisUntilFinished) {

        }

        public void onFinish() {
            detectedLayout.setVisibility(View.INVISIBLE);
            sensingLayout.setVisibility(View.VISIBLE);
        }
    }.start();
}
 
源代码27 项目: VideoRecord   文件: MediaRecorderBase.java
/** 
 * 测试PreviewFrame回调次数,时间1分钟
 * 
 */
public void testPreviewFrameCallCount() {
	new CountDownTimer(1 * 60 * 1000, 1000) {

		@Override
		public void onTick(long millisUntilFinished) {
			android.util.Log.e("[Vitamio Recorder]", "testFrameRate..." + mPreviewFrameCallCount);
			mPreviewFrameCallCount = 0;
		}

		@Override
		public void onFinish() {

		}

	}.start();
}
 
源代码28 项目: DebugRank   文件: TimerTextView.java
public void startTimer()
{
    countDownTimer = new CountDownTimer(timeInSeconds * 1000, 1000)
    {
        @Override
        public void onTick(long millisUntilFinished)
        {
            if (canUpdate.get())
            {
                timeInSeconds = (int) Math.floor(millisUntilFinished / 1000);

                setTimeText();
            }
        }

        @Override
        public void onFinish()
        {
            timeInSeconds = 0;
            setTimeText();
            timerCompletedListener.timeRanOut();
        }
    }.start();
}
 
源代码29 项目: AndroidStudyDemo   文件: CountDownButtonHelper.java
/**
 * @param button 需要显示倒计时的Button
 * @param defaultString 默认显示的字符串
 * @param max 需要进行倒计时的最大值,单位是秒
 * @param interval 倒计时的间隔,单位是秒
 */
public CountDownButtonHelper(final Button button,
                             final String defaultString, int max, int interval) {
    this.mCountDownBtn = button;
    // 由于CountDownTimer并不是准确计时,在onTick方法调用的时候,time会有1-10ms左右的误差,这会导致最后一秒不会调用onTick()
    // 因此,设置间隔的时候,默认减去了10ms,从而减去误差。
    // 经过以上的微调,最后一秒的显示时间会由于10ms延迟的积累,导致显示时间比1s长max*10ms的时间,其他时间的显示正常,总时间正常
    mCountDownTimer = new CountDownTimer(max * 1000, interval * 1000 - 10) {
        @Override
        public void onTick(long time) {
            // 第一次调用会有1-10ms的误差,因此需要+15ms,防止第一个数不显示,第二个数显示2s
            button.setText(defaultString + "(" + ((time + 15) / 1000)
                    + "秒)");
        }

        @Override
        public void onFinish() {
            button.setEnabled(true);
            button.setText(defaultString);
            if (mOnFinishListener != null) {
                mOnFinishListener.finish();
            }
        }
    };
}
 
源代码30 项目: ClockView   文件: FloatWaitView.java
private void createTimer() {
    timer = new CountDownTimer(duration, duration / (ovalNum + 1)) {
        @Override
        public void onTick(long millisUntilFinished) {
            if (position < ovalNum) {
                Oval oval = mOvals.get(position++);
                oval.mAnimator.start();
            }
        }

        @Override
        public void onFinish() {

        }
    };
    timer.start();
}
 
源代码评论
动弹
沙发等你来抢
 类所在包
 类方法
 同包方法