上,我试图让这个倒计时到以天/小时/分/秒的事件的一天算。我需要更改此代码,以便在启动时使用当前的android手机时间和日期,以根据手机显示的时间和日期显示剩余时间。目前,开始时间和日期是由我设定,每安装并模拟器运行应用时,它显示34天所以如果我安装和运行它明天不会显示33天但有34天后再次..我找不到任何问题有一个有用的答案,我在过去几天看过几个地方。这里是我的事件活动java,我在相对布局中使用TextView。倒计时开始在指定的时间和日期,我需要用时间和日期的android手机
extends Activity {
Button button01;
CountDownTimer mCountDownTimer;
TextView mTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event_activity);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy, hh:mm");
Date a = null, b = null;
try {
a = sdf.parse("24-02-2014, 14:30");
b = sdf.parse("06-01-2014, 02:04");
} catch (ParseException e) {
e.printStackTrace();
}
mTextView = (TextView) findViewById(R.id.timer);
mCountDownTimer = new CountDownTimer(a.getTime() - b.getTime(), 1000) {
StringBuilder time = new StringBuilder();
@Override
public void onFinish() {
mTextView.setText(DateUtils.formatElapsedTime(0));
//mTextView.setText("Times Up!");
}
@Override
public void onTick(long millisUntilFinished) {
time.setLength(0);
// Use days if appropriate
if(millisUntilFinished > DateUtils.DAY_IN_MILLIS) {
long count = millisUntilFinished/DateUtils.DAY_IN_MILLIS;
if(count > 1)
time.append(count).append(" days, ");
else
time.append(count).append(" day, ");
millisUntilFinished %= DateUtils.DAY_IN_MILLIS;
}
if(millisUntilFinished > DateUtils.HOUR_IN_MILLIS) {
long count2 = millisUntilFinished/DateUtils.HOUR_IN_MILLIS;
if(count2 > 1)
time.append(count2).append(" hours, ");
else
time.append(count2).append(" hour, ");
millisUntilFinished %= DateUtils.HOUR_IN_MILLIS;
}
if(millisUntilFinished > DateUtils.MINUTE_IN_MILLIS) {
long count3 = millisUntilFinished/DateUtils.MINUTE_IN_MILLIS;
if(count3 > 1)
time.append(count3).append(" minutes, ");
else
time.append(count3).append(" minute, ");
millisUntilFinished %= DateUtils.MINUTE_IN_MILLIS;
}
time.append(DateUtils.formatElapsedTime(Math.round(millisUntilFinished/1000d)));
mTextView.setText(time.toString());
}
}.start();
}
请帮帮忙,我怎么能得到这个更新,而不是将开始日期和时间自己当前的时间!它在运行模拟器时没有显示正确的开始时间,因为它需要我运行它。由于
-KG