我正在尝试创建一个适用于Android 2.2(API 8)的倒数计时器。我找到了这个代码,但它只适用于API 9及更高版本。如何在API 8中使用此代码?
//API 9 and above
public class CountClass extends CountDownTimer{
public CountClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
txtchronoview.setText("Complete.");
Toast.makeText(calculator1.this,
"Call a judge, your opponent is exceed its 3 minutes limit per turn", Toast.LENGTH_SHORT).show();
vibrate(3000);
//call();
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
(TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toMinutes(millis)) / 60000 ), //DISPLAY 3 MINUTE COUNT DOWN
//(TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toMinutes(millis)) / 60 ), //DISPLAY 1 MINUTE COUNT DOWN CYCLE FOR 3 TIMES
//(TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toMinutes(millis)) ), //DISPLAY EVERYTHING IN MILLISECONDS FOR 3 MINUTES
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)) );
System.out.println(hms);
txtchronoview.setText(hms);
}
}
答案 0 :(得分:1)
在min和sec中使用它,稍微改变一下就可以增加小时。
public String milisecToMinAndSec(long milisec) {
int sec = (int) milisec / 1000;
String outString = "";
if (sec > 60)
outString = String.format("%02d",(int) (sec / 60)) + ":" + String.format("%02d",(int) (sec % 60));
else
outString = "00:" + String.format("%02d",sec);
return outString;
}