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 表示给定粒度单位的持续时间,并提供实用方法来跨单位转换,并在这些单位中执行计时和延迟操作。
对于 1.5 以下的 Java 版本或不完全支持 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);
}