我正在开发测验应用程序。我需要为此保留一个计时器,如果单击暂停按钮按钮,则计时器应停止,如果再次单击播放按钮,则计时器应从先前停止的位置开始。现在我的问题是当我点击暂停按钮时计时器没有停止。请问有人可以检查这个问题吗?真的会很感谢...
My Code:
public void onCreate(Bundle savedInstanceState) {
timervalue = 3 * 80000
btn = (ImageView) findViewById(R.id.imageView2);
btn.setImageResource(R.drawable.pause);
btn.setVisibility(View.VISIBLE);
}
LongOperation1 op = new LongOperation1();
op.execute("");
}
private class LongOperation1 extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
loadFeed2();
return "Executed";
}
@Override
protected void onPostExecute(String result) {
dialog1.dismiss();
try {
if (modeprefName.equals("timed")) {
counter = new MyCount(timervalue, 1000);
counter.start();
}
setquestion();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onPreExecute() {
dialog1 = ProgressDialog.show(Taketest.this, "Please wait...",
"Retrieving data ...", true);
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
@Override
public void onClick(View v) {
if (b == false) {
b = true;
counter.cancel();
btn.setImageResource(R.drawable.play);
} else {
b = false;
counter = new MyCount(s1, 1000);
counter.start();
btn.setImageResource(R.drawable.pause);
}
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
time.setText("DONE");
}
@Override
public void onTick(long millisUntilFinished) {
s1 = millisUntilFinished;
timervalue = s1;
ss1 = millisUntilFinished / 1000;
String seconds = String
.valueOf((millisUntilFinished / (1000)) % 60);
String minutes = String.valueOf((millisUntilFinished / 60000) % 60);
String hours = String.valueOf(millisUntilFinished / 3600000);
if (seconds.length() > 1) {
// time.setText("Time Left: "+ minutes+":"+seconds);
} else if (seconds.length() <= 1) {
seconds = "0" + seconds;
}
if (minutes.length() > 1) {
} else if (minutes.length() <= 1) {
minutes = "0" + minutes;
}
if (hours.length() > 1) {
} else if (hours.length() <= 1) {
hours = "0" + hours;
}
time.setText("Time: " + hours + ":" + minutes + ":" + seconds);
}
}