How to extend CountDown Timer With Pause?

2019-08-23 12:07发布

This seems to be a common question, "how do I pause a countdowntimer"?

I found this code which looks promising:

http://www.java2s.com/Open-Source/Android/Timer/multitimer-android/com/cycleindex/multitimer/CountDownTimerWithPause.java.htm

I'm just not sure how to implement it in code. I need to override the abstracted methods and extend the class. Can someone give me an idea how to do this?

2条回答
霸刀☆藐视天下
2楼-- · 2019-08-23 12:38

Here is your layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="0"
            android:gravity="center_horizontal"
            android:layout_marginBottom="40dp"
            android:layout_marginTop="20dp"
            android:textSize="27dp"
            android:id="@+id/txtView"/>
    <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_gravity="center">
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Start"
                android:id="@+id/startButton"/>
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Stop"
                android:id="@+id/stopButton"
                android:layout_toRightOf="@id/startButton"/>
    </RelativeLayout>
</LinearLayout>


Here is your activity class

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MyActivity extends Activity implements View.OnClickListener {

    TextView txt = null;
    Button startButton = null;
    Button stopButton = null;
    CountDownTimerWithPause timer = null;

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

        txt = (TextView) findViewById(R.id.txtView);
        startButton = (Button) findViewById(R.id.startButton);
        stopButton = (Button) findViewById(R.id.stopButton);

        startButton.setOnClickListener(this);
        stopButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view == startButton) {
            if (timer == null) {
                initTimer();
            }

            timer.resume();
        }

        if (view == stopButton) {
            timer.pause();
        }
    }

    private void initTimer() {
        timer = new CountDownTimerWithPause(10000, 1000, false) {
            @Override
            public void onTick(long millisUntilFinished) {
                Integer currentValue = Integer.valueOf((String) MyActivity.this.txt.getText());
                MyActivity.this.txt.setText(String.valueOf(currentValue + 1));
            }

            @Override
            public void onFinish() {
                Toast.makeText(MyActivity.this, "finish", Toast.LENGTH_SHORT).show();
            }
        };

        timer.create();
    }
}

Do not forget to copy CountDownTimerWithPause into your package.

Works for me, BUT...

I mention some bugs but it depends on your application. Maybe it's OK for you. Bug is - when starting timer, it fires immediatelly. 'So what?' you might ask. Imagine you click Stop button when 3 and half seconds passed. So when you click Start button you expect half second pass before you see 4, but not in that class implementation.

And there is some strange delay at the end of counting. I'd recommend to search for better implementation.

查看更多
太酷不给撩
3楼-- · 2019-08-23 12:44

I have used CountDownTimerWithPause in my projects and it is pretty simple. Take a look on this sample code I just wrote to explain the implementation of this counter.

Step 1

I have created sample XML layout just for better explanation later. This layout contains 3 buttons:

  1. Start
  2. Pause
  3. Resume

The layout:

<Button
    android:id="@+id/button_start"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start" />

<Button
    android:id="@+id/button_pause"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Pause" />

<Button
    android:id="@+id/button_resume"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Resume" />

Step 2

Just for this example I worked within an Activity. So this is the code:

public class MainActivity extends Activity
{

    private CountDownTimerWithPause _countDownTimerWithPause = null;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.timer);

        /*
         * Definition of the counter
         */
        _countDownTimerWithPause = new CountDownTimerWithPause(10000, 1000, true)
        {

            @Override
            public void onTick(long millisUntilFinished)
            {
                Toast.makeText(getApplicationContext(), "Tick", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish()
            {
                Toast.makeText(getApplicationContext(), "Finished", Toast.LENGTH_SHORT).show();
            }
        };

        /*
         * Click to start the counter
         */
        Button buttonStart = (Button) findViewById(R.id.button_start);
        buttonStart.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                _countDownTimerWithPause.create();
            }
        });

        /*
         * Click to pause the counter
         */
        Button buttonPause = (Button) findViewById(R.id.button_pause);
        buttonPause.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                _countDownTimerWithPause.pause();
            }
        });

        /*
         * Click to resume the counter
         */
        Button buttonResume = (Button) findViewById(R.id.button_resume);
        buttonResume.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                _countDownTimerWithPause.resume();
            }
        });
    }

}

It is easy to see how I have extended the CountDownTimerWithPause and which methods should be called to start,pause and resume the counter.

Hope I could explain the usage of this counter.

查看更多
登录 后发表回答