http://developer.android.com/reference/android/os/CountDownTimer.html
TextView _tv = (TextView) findViewById(R.id.textView1);
new CountDownTimer(30000, 1000) { // adjust the milli seconds here
public void onTick(long millisUntilFinished) {
_tv.setText(""+String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
public void onFinish() {
_tv.setText("done!");
}
}.start();
由於Java 1.5存在java.util.concurrent.TimeUnit中類
_tv.setText(""+String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/TimeUnit.html
TimeUnit表示在給定的單位時間段的粒度,並提供實用的方法來轉換acr oss單元,並在這些單元中執行計時和延遲操作。
對於Java版本低於1.5或者不完全支持TIMEUNIT類下面的公式可用於系統:
int seconds = (int) (milliseconds/1000) % 60 ;
int minutes = (int) ((milliseconds/(1000*60)) % 60);
int hours = (int) ((milliseconds/(1000*60*60)) % 24);
振動
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(500);
<uses-permission android:name="android.permission.VIBRATE"/> // permission in manifest
編輯:
震動在4分55秒500毫秒
if((TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)==4) &&
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))==55)
{
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(500);
}
問題在哪裏? – amalBit
現在是/現在不行嗎? – codeMagic