我正在尝试制作一个简单的倒数计时器,显示直到某个事件的天数小时和秒,我已经让这个代码到目前为止工作,但它以2秒的间隔倒计时,而不是一个EX:(10 ,8,6,4 ...)我很困惑为什么它这样做,我想我所有的时间计算都是正确的,有什么我想念的吗?它每1秒计算2秒......
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.R.string;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
TextView tv;
long diff;
long milliseconds;
long endTime;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.textView2);
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
formatter.setLenient(false);
String oldTime = "13.11.2014, 00:00";
Date oldDate;
try {
oldDate = formatter.parse(oldTime);
milliseconds = oldDate.getTime();
//long startTime = System.currentTimeMillis();
// do your work...
long endTime=System.currentTimeMillis();
diff = endTime-milliseconds;
Log.e("day", "miliday"+diff);
long seconds = (long) (diff / 1000) % 60 ;
Log.e("secnd", "miliday"+seconds);
long minutes = (long) ((diff / (1000*60)) % 60);
Log.e("minute", "miliday"+minutes);
long hours = (long) ((diff / (1000*60*60)) % 24);
Log.e("hour", "miliday"+hours);
long days = (int)((diff / (1000*60*60*24)) % 365);
Log.e("days", "miliday"+days);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Long serverUptimeSeconds = (System.currentTimeMillis() - milliseconds) / 1000;
String serverUptimeText = String.format("%d days %d hours %d minutes %d seconds",
serverUptimeSeconds / 86400,
( serverUptimeSeconds % 86400) / 3600 ,
((serverUptimeSeconds % 86400) % 3600 ) / 60,
((serverUptimeSeconds % 86400) % 3600 ) % 60
);
Log.v("jjj", "miliday"+serverUptimeText);
MyCount counter = new MyCount(milliseconds,1000);
counter.start();
}
// countdowntimer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
tv.setText("done!");
}
@Override
public void onTick(long millisUntilFinished) {
//tv.setText("Left: " + millisUntilFinished / 1000);
long diff = endTime - millisUntilFinished;
Log.e("left", "miliday"+diff);
long seconds = (long) (diff / 1000) % 60 ;
//Log.e("secnd", "miliday"+seconds);
long minutes = (long) ((diff / (1000*60)) % 60);
//Log.e("minute", "miliday"+minutes);
long hours = (long) ((diff / (1000*60*60)) % 24);
//Log.e("hour", "miliday"+hours);
int days = (int)((diff / (1000*60*60*24)) % 365);
Log.v("days", "miliday"+days);
Long serverUptimeSeconds =
(System.currentTimeMillis() - millisUntilFinished) / 1000;
String serverUptimeText =
String.format("%d days %d hours %d minutes %d seconds",
serverUptimeSeconds / 86400,
( serverUptimeSeconds % 86400) / 3600 ,
((serverUptimeSeconds % 86400) % 3600 ) / 60,
((serverUptimeSeconds % 86400) % 3600 ) % 60
);
Log.v("new its", "miliday"+serverUptimeText);
// tv.setText(days +":"+hours+":"+minutes + ":" + seconds);
tv.setText(serverUptimeText);
}
}
}
答案 0 :(得分:0)
这是我正在使用的代码 公共类MainActivity扩展Activity { TextView dayView; TextView timeView; TextView quoteView; 长差异; long endMilliseconds; long endTime; int min = 1; int max = 445; int randomNumber = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dayView = (TextView) findViewById(R.id.textViewDateDays);
timeView = (TextView) findViewById(R.id.textViewDateTime);
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
formatter.setLenient(false);
String endTime = "7.08.2015, 00:00";
Date endDate;
try {
endDate = formatter.parse(endTime);
endMilliseconds = endDate.getTime();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MyCount counter = new MyCount(endMilliseconds - System.currentTimeMillis(),1000);
counter.start();
}
// countdowntimer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
dayView.setText("done!");
}
@Override
public void onTick(long millisUntilFinished) {
Long serverUptimeSeconds = millisUntilFinished / 1000;
String serverUptimeText =
String.format("%d Hours : %d Minutes : %d Seconds",
(serverUptimeSeconds % 86400) / 3600 ,
((serverUptimeSeconds % 86400) % 3600 ) / 60,
((serverUptimeSeconds % 86400) % 3600 ) % 60
);
dayView.setText(Long.toString(serverUptimeSeconds / 86400));
timeView.setText(serverUptimeText);
}
}
}