0

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);

    }
}
Amrutha
  • 575
  • 4
  • 9
  • 29
  • You can have multiple timer objects at the same time, you should limit them to one object instead. why do you use counter object in your AsynchTask ? – Mr.Me Jun 17 '13 at 08:29
  • Did you search before you ask? simialr question - http://stackoverflow.com/questions/4993765/how-to-stop-the-timer-in-android – Gina Jun 17 '13 at 08:44
  • Hi, since mine is a quiz application I need to put a timer. I am setting the questions and options in onpostexecute method so before setting the questions i started the timer and then setting the questions by calling setquestion method. – Amrutha Jun 17 '13 at 08:46

0 Answers0