I am working on quiz application. I need to keep a timer for that and if the pause button button is clicked the timer should stop and if play button is clicked again the timer should start from where it is stopped previously. Now my problem is when I click on the pause button the timer is not stopping. Please can anybody check this issue? Will be really thankful...
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);
}
}