在 activity 中显示计时器
Show timer in activity
我在 activity 中创建了一个计时器,有时它工作得很好,但有时它 运行 比指定的执行速度更快 period.Here 是我的代码
public void StartTimer()
{try
{
// Start Timer
Log.e("", "time1 : " + AppConstants.TOTALTIME);
int millisUntilFinished = Integer.parseInt(AppConstants.TOTALTIME) * 60000;
final String hh = String.format("%02d",(int) ((millisUntilFinished / (1000 * 60 * 60)) % 24));
final String mm = String.format("%02d", (millisUntilFinished / 60000) % 60);
final String ss = (String.format("%02d",(millisUntilFinished % 60000 / 1000)));
Log.e("", "time : " + hh + ":" + mm + ":" + ss);
txtTime.setText(hh + ":" + mm + ":" + ss);
txtTimeSpent.setText("00:00:00");
t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (txtTimeSpent.getText().toString().trim().equalsIgnoreCase(hh + ":" + mm + ":" + ss))
{
if (t != null)
t.cancel();
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss a");
String enddate = sdf.format(c.getTime());
AppConstants.ENDTIME = enddate;
Intent intent = new Intent(act,TestSubmissionActivity.class);
startActivity(intent);
finish();
}
else
{
txtTimeSpent.setText(" "
+ String.format("%02d", hrSpent) + ":"
+ String.format("%02d", minSpent) + ":"
+ String.format("%02d", secSpent));
AppConstants.TIMETAKEN = txtTimeSpent.getText().toString().trim();
secSpent++;
if (secSpent == 60)
{
txtTimeSpent.setText(" "
+ String.format("%02d", hrSpent) + ":"
+ String.format("%02d", minSpent) + ":"
+ String.format("%02d", secSpent));
AppConstants.TIMETAKEN = txtTimeSpent.getText().toString().trim();
secSpent = 0;
minSpent++;
if (minSpent == 60)
{
txtTimeSpent.setText(" "
+ String.format("%02d", hrSpent)
+ ":"
+ String.format("%02d", minSpent)
+ ":"
+ String.format("%02d", secSpent));
AppConstants.TIMETAKEN = txtTimeSpent.getText().toString().trim();
minSpent = 0;
hrSpent++;
}
}
CountTimeSpend++;
if(CountTimeSpend==5)
{
CountTimeSpend=0;
try {
Date d=sdfDate.parse((sdfDate.format(c.getTime())+" "+txtTimeSpent.getText().toString().trim()));
new AsyncSaveTimeSpend().execute(d);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
}
});
}
}, 0, 1000);
}
catch(Exception e)
{
e.printStackTrace();
}
}
StartTimer() 在 AsyncTask 中被调用。这是 ondestroy()
@Override
protected void onDestroy() {
super.onDestroy();
if (t != null) {
t.cancel();
t.purge();
t = null;
}
}
找到了我的问题的解决方案。不知道以前的方法有什么问题,但 CountDownTimer
工作得很好。这是我的解决方案
public void StartTimer()
{
final long millisTotalTime = Integer.parseInt(AppConstants.TOTALTIME) * 60000;
hh = String.format("%02d",(int) ((millisTotalTime / (1000 * 60 * 60)) % 24));
mm = String.format("%02d", (millisTotalTime / 60000) % 60);
ss = (String.format("%02d",(millisTotalTime % 60000 / 1000)));
Log.e("", "time : " + hh + ":" + mm + ":" + ss);
txtTime.setText(hh + ":" + mm + ":" + ss);
txtTimeSpent.setText("00:00:00");
millisSpent=0;
countDownTimer=new CountDownTimer(millisTotalTime, 1000)
{
@Override
public void onTick(long millisUntilFinished)
{
millisSpent=millisTotalTime-millisUntilFinished;
hh = String.format("%02d",(int) ((millisSpent / (1000 * 60 * 60)) % 24));
mm = String.format("%02d", (millisSpent / 60000) % 60);
ss = (String.format("%02d",(millisSpent % 60000 / 1000)));
txtTimeSpent.setText( hh + ":"+ mm + ":"+ ss);
System.out.println("Time Spent=="+txtTimeSpent.getText());
AppConstants.TIMETAKEN = txtTimeSpent.getText().toString().trim();
CountTimeSpend++;
if(CountTimeSpend==5)
{
CountTimeSpend=0;
try {
Date d=sdfDate.parse((sdfDate.format(c.getTime())+" "+txtTimeSpent.getText().toString().trim()));
new AsyncSaveTimeSpend().execute(d);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
@Override
public void onFinish() {
Calendar c = Calendar.getInstance();
String enddate = sdf.format(c.getTime());
AppConstants.ENDTIME = enddate;
Intent intent = new Intent(act,TestSubmissionActivity.class);
act.startActivity(intent);
act.finish();
}
}.start();
}
如果你想在指定时间之前取消 CountDownTimer
那么只需调用 countDownTimer.cancel();
希望它能帮助一些人:)
我在 activity 中创建了一个计时器,有时它工作得很好,但有时它 运行 比指定的执行速度更快 period.Here 是我的代码
public void StartTimer()
{try
{
// Start Timer
Log.e("", "time1 : " + AppConstants.TOTALTIME);
int millisUntilFinished = Integer.parseInt(AppConstants.TOTALTIME) * 60000;
final String hh = String.format("%02d",(int) ((millisUntilFinished / (1000 * 60 * 60)) % 24));
final String mm = String.format("%02d", (millisUntilFinished / 60000) % 60);
final String ss = (String.format("%02d",(millisUntilFinished % 60000 / 1000)));
Log.e("", "time : " + hh + ":" + mm + ":" + ss);
txtTime.setText(hh + ":" + mm + ":" + ss);
txtTimeSpent.setText("00:00:00");
t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (txtTimeSpent.getText().toString().trim().equalsIgnoreCase(hh + ":" + mm + ":" + ss))
{
if (t != null)
t.cancel();
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss a");
String enddate = sdf.format(c.getTime());
AppConstants.ENDTIME = enddate;
Intent intent = new Intent(act,TestSubmissionActivity.class);
startActivity(intent);
finish();
}
else
{
txtTimeSpent.setText(" "
+ String.format("%02d", hrSpent) + ":"
+ String.format("%02d", minSpent) + ":"
+ String.format("%02d", secSpent));
AppConstants.TIMETAKEN = txtTimeSpent.getText().toString().trim();
secSpent++;
if (secSpent == 60)
{
txtTimeSpent.setText(" "
+ String.format("%02d", hrSpent) + ":"
+ String.format("%02d", minSpent) + ":"
+ String.format("%02d", secSpent));
AppConstants.TIMETAKEN = txtTimeSpent.getText().toString().trim();
secSpent = 0;
minSpent++;
if (minSpent == 60)
{
txtTimeSpent.setText(" "
+ String.format("%02d", hrSpent)
+ ":"
+ String.format("%02d", minSpent)
+ ":"
+ String.format("%02d", secSpent));
AppConstants.TIMETAKEN = txtTimeSpent.getText().toString().trim();
minSpent = 0;
hrSpent++;
}
}
CountTimeSpend++;
if(CountTimeSpend==5)
{
CountTimeSpend=0;
try {
Date d=sdfDate.parse((sdfDate.format(c.getTime())+" "+txtTimeSpent.getText().toString().trim()));
new AsyncSaveTimeSpend().execute(d);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
}
});
}
}, 0, 1000);
}
catch(Exception e)
{
e.printStackTrace();
}
}
StartTimer() 在 AsyncTask 中被调用。这是 ondestroy()
@Override
protected void onDestroy() {
super.onDestroy();
if (t != null) {
t.cancel();
t.purge();
t = null;
}
}
找到了我的问题的解决方案。不知道以前的方法有什么问题,但 CountDownTimer
工作得很好。这是我的解决方案
public void StartTimer()
{
final long millisTotalTime = Integer.parseInt(AppConstants.TOTALTIME) * 60000;
hh = String.format("%02d",(int) ((millisTotalTime / (1000 * 60 * 60)) % 24));
mm = String.format("%02d", (millisTotalTime / 60000) % 60);
ss = (String.format("%02d",(millisTotalTime % 60000 / 1000)));
Log.e("", "time : " + hh + ":" + mm + ":" + ss);
txtTime.setText(hh + ":" + mm + ":" + ss);
txtTimeSpent.setText("00:00:00");
millisSpent=0;
countDownTimer=new CountDownTimer(millisTotalTime, 1000)
{
@Override
public void onTick(long millisUntilFinished)
{
millisSpent=millisTotalTime-millisUntilFinished;
hh = String.format("%02d",(int) ((millisSpent / (1000 * 60 * 60)) % 24));
mm = String.format("%02d", (millisSpent / 60000) % 60);
ss = (String.format("%02d",(millisSpent % 60000 / 1000)));
txtTimeSpent.setText( hh + ":"+ mm + ":"+ ss);
System.out.println("Time Spent=="+txtTimeSpent.getText());
AppConstants.TIMETAKEN = txtTimeSpent.getText().toString().trim();
CountTimeSpend++;
if(CountTimeSpend==5)
{
CountTimeSpend=0;
try {
Date d=sdfDate.parse((sdfDate.format(c.getTime())+" "+txtTimeSpent.getText().toString().trim()));
new AsyncSaveTimeSpend().execute(d);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
@Override
public void onFinish() {
Calendar c = Calendar.getInstance();
String enddate = sdf.format(c.getTime());
AppConstants.ENDTIME = enddate;
Intent intent = new Intent(act,TestSubmissionActivity.class);
act.startActivity(intent);
act.finish();
}
}.start();
}
如果你想在指定时间之前取消 CountDownTimer
那么只需调用 countDownTimer.cancel();
希望它能帮助一些人:)