0

In my application I kept pause button and start button. By default I set the timer to 50sec. When the timer is running I wrote the onclick action code for each button...i.e when pause button is clicked the time is pausing and again when start button is clicked it is again starting from 50sec. But it should start from the time where it is paused. How can I do that?

Please help me....thanks in advance.......

My Code:

long timervalue = 50000;

btn13 = (ImageView) findViewById(R.id.pause);
btn14 = (ImageView) findViewById(R.id.resume);

final CountDownTimer Counter1 = new CountDownTimer(timervalue, 1000) {
        public void onTick(long millisUntilFinished) {
            time.setText(formatTime(millisUntilFinished));

            btn13.setOnClickListener(new View.OnClickListener() 
            {
                public void onClick(View v)
                {  
                    Counter1.cancel();
                }

     });
            btn14.setOnClickListener(new View.OnClickListener() 
            {
                public void onClick(View v)
                {  

                    Counter1.start();

                }

     });

        }
public void onFinish()
 {
    Counter1.cancel();
}
};



public String formatTime(long millis) {
        String output = "00:00";
        long seconds = millis / 1000;
        // long minutes = seconds / 60;

        seconds = seconds % 60;
        // minutes = minutes % 60;

        String secondsD = String.valueOf(seconds);
        // String minutesD = String.valueOf(minutes);

        if (seconds < 10)
            secondsD = "0" + seconds;
        // if (minutes < 10)
        // minutesD = "0" + minutes;

        output = secondsD;
        return output;
    }
RaagaSudha
  • 397
  • 5
  • 17
  • 38

1 Answers1

0

Unfortunately, you cannot pause CountDownTimer, but there are other ways to do what you want to do.

try this:

public class HelloWorldActivity extends Activity {
    /** Called when the activity is first created. */
    private CountDownTimer ct;
    private long ts = 30;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final TextView mTextField = (TextView) this.findViewById(R.id.txt);
        final Button bt = (Button) this.findViewById(R.id.bt);

         bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(bt.getText().toString().equals("Start")){
                    bt.setText("Stop");
                     ct =  new CountDownTimer(ts*1000, 1000) {

                            public void onTick(long millisUntilFinished) {
                                mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
                                ts = millisUntilFinished/1000;
                            }

                            public void onFinish() {
                                mTextField.setText("done!");
                            }
                         }.start();
                }
                else{
                    bt.setText("Start");
                    ct.cancel();}
            }
        });
   }
}
  • hi thanks for the response..I tried your code but it is showing error at line ct.cancel();....Also I edited my code...please check it once... – RaagaSudha Mar 07 '12 at 10:04