开发者

How to start and stop Timer in Android? [closed]

开发者 https://www.devze.com 2023-02-20 04:48 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. 开发者_运维百科

Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist

Closed 9 years ago.

Improve this question

Please any one tell me how to start the Timer and How to stop that


Here you find all the necessary information. As you can see there are various methods to start your timer. Just use the appropriate schedule(...) method. In order to stop the timer you can use the cancel() method and afterwards the purge() method. But be aware that the cancel() method cancels all tasks that are associated with this timer.


Are you talking about the Timer class?

You can use the schedule methods to schedule a TimerTask and TimerTask#cancel to cancel it.


If you want to stop the countdown timer..

Timer.cancel();


Use this Working Code.
    package com.example.stopwatch;

   import android.app.Activity;
   import android.os.Bundle;
   import android.os.SystemClock;
   import android.util.Log;

   import android.widget.Button;
   import android.widget.Chronometer;
   import android.widget.CompoundButton;

    import android.widget.ToggleButton;

    public class StopWatch extends Activity {
    Chronometer mChronometer;

    Button button;
    private long start = SystemClock.elapsedRealtime();
    private long stop = 0;
    private long totalPauseTime = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

            try {
                super.onCreate(savedInstanceState);

                setContentView(R.layout.main);


                final ToggleButton toggle = (ToggleButton)           findViewById(R.id.toggleButton1);
                final Chronometer chrono = (Chronometer) findViewById(R.id.chronometer1);

                final ToggleButton toggle2 = (ToggleButton)        findViewById(R.id.toggleButton2);
                final Chronometer chrono2 = (Chronometer)           findViewById(R.id.chronometer2);

      toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
     @Override
     public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                try {
                    Log.i("[chrono]", String.format("start:%d stop:%d delta: %d",start, stop, start - stop));
                    Log.i("[chrono]", "base:" +chrono.getBase());
                    Log.i("[chrono]", "elapsed time:" +SystemClock.elapsedRealtime());

                    if (isChecked) {
                        chrono.start();
                        start = SystemClock.elapsedRealtime();
                        chrono.setBase(start - totalPauseTime);
                    } else {
                        totalPauseTime +=    SystemClock.elapsedRealtime() - start;
                        chrono.stop();
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

  }
   });

     toggle2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      @Override
     public void onCheckedChanged(CompoundButton buttonView,
                      boolean isChecked) {
                 try {
                    Log.i("[chrono]", String.format("start:%d    stop:%d         delta: %d",start, stop, start - stop));
                    Log.i("[chrono]", "base:" +chrono.getBase());
                    Log.i("[chrono]", "elapsed time:"   +SystemClock.elapsedRealtime());

                      if (isChecked) {
                          chrono2.start();
                          start = SystemClock.elapsedRealtime();
                          chrono2.setBase(start - totalPauseTime);
                    } else {
                        totalPauseTime +=                 SystemClock.elapsedRealtime() - start;
                        chrono2.stop();
                    }
                } catch (Exception e) {


    enter code here

        // TODO Auto-generated catch block
                    e.printStackTrace();
                }

    }
    });
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }       }
0

精彩评论

暂无评论...
验证码 换一张
取 消