我有个问题。我想在特定时间启动计时器。即,我有时间说,05:10:02。那么计时器将从 05:10:02 开始。例如,05:10:02、05:10:03、05:10:04 等等。
当我打开活动时,它应该获取当前时间,并且计时器将启动上述问题中的描述。
我有以下代码。
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
inputString = str; //05:10:02
try {
date = sdf.parse(inputString);
Log.i("Test", "in milliseconds: " + date.getTime());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(mStartTimeGlobal== 0L) {
mStartTimeGlobal = date.getTime();
mHandlerGlobal.removeCallbacks(mUpdateTimeTaskGlobal);
mHandlerGlobal.postDelayed(mUpdateTimeTaskGlobal, 100);
}
private Runnable mUpdateTimeTaskGlobal = new Runnable(){
public void run() {
//DecimalFormat df = new DecimalFormat("##.####");
final long start = mStartTimeGlobal;
long millis = date.getTime();
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
int hours = seconds/3600;
String timer = String.format("%02d", hours) + ":" + String.format("%02d", minutes) + ":" + String.format("%02d", seconds);
///mTotalTimeTakenGlobal = timer;
txt_start_timer.setText(timer);
mHandlerGlobal.postDelayed(this, 200);
}
};