// Comment private MyCoundDownTask mytask; private boolean mytaskStop = true; private ProgressDialog progressDialog; private void startCountDown() { progressDialog = ProgressDialog.show(context, "Processing", "Please wait..."); mytaskStop = false; mytask = new MyCoundDownTask(20000, 1000); mytask.start(); Log.e("startCountDown", "開始"); } private void cancelCountDown() { mytaskStop = true; if (mytask != null) { mytask.cancel(); mytask=null; Log.e("cancelCountDown", "提早結束"); } } private class MyCoundDownTask extends CountDownTimer { public MyCoundDownTask(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } @Override public void onTick(long millisUntilFinished) { int restSec = (int) (millisUntilFinished / 1000); Log.e("CountDownTimer", "剩下秒數:" + restSec); progressDialog.setMessage("Please wait for:" + (millisUntilFinished / 1000) + " sec"); } @Override public void onFinish() { mytaskStop = true; if (progressDialog != null) { progressDialog.dismiss(); } Log.e("CountDownTimer", "時間到"); } }
2015年3月17日 星期二
[Android] CountDownTimer
new MyCoundDownTask(20000, 1000),從20秒倒數至1秒,progressDialog所顯示的秒數會自動更新。
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言