2 回答
TA贡献2011条经验 获得超2个赞
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);
}
添加回答
举报