java - 如何在倒数计时器中实现暂停和恢复方法

标签 java android countdowntimer onresume onpause

您好,我正在研究倒计时概念。在这里,当我单击开始按钮时,倒计时将开始。以及停止它的工作正常,但我需要在我的倒数计时器中实现暂停和恢复方法。当我单击暂停按钮时,倒计时将进入暂停状态。当我单击开始按钮时,倒计时将从暂停值开始。例如:我的倒计时是 0 到 30,假设如果我单击暂停按钮,值将暂停,例如:25 或 15 其他。当我点击开始按钮时,倒计时将从 25 开始。我希望你们都能理解我的问题,我试过了,但我没有得到任何解决方案,你们能帮帮我吗

  public class MainActivity extends ActionBarActivity implements OnClickListener {

     private CountDownTimer countDownTimer;
     private boolean timerHasStarted = false;
     private Button startB;
     public TextView text;
     private final long startTime = 30 * 1000;
     private final long interval = 1 * 1000;


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

    startB = (Button) this.findViewById(R.id.button);
    startB.setOnClickListener((OnClickListener) this);
    text = (TextView) this.findViewById(R.id.timer);
    countDownTimer = new MyCountDownTimer(startTime, interval);
    text.setText(text.getText() + String.valueOf(startTime / 1000));
   }

    public void onClick(View v) {
     if (!timerHasStarted) {
      countDownTimer.start();
      timerHasStarted = true;
      startB.setText("STOP");
      } else {
        countDownTimer.cancel();
        timerHasStarted = false;
        startB.setText("RESTART");
    }
    } 
 public class MyCountDownTimer extends CountDownTimer {
   public MyCountDownTimer(long startTime, long interval) {
                   super(startTime, interval);
  }

   @Override
    public void onFinish() {
     //text.setText("Time's up!");
      countDownTimer.start();
   }

     @Override
      public void onTick(long millisUntilFinished) {
      text.setText("" + millisUntilFinished / 1000);
         }
       }
    }


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:id="@+id/rl" >

    <TextView
    android:id="@+id/timer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:paddingRight="10dip"
    android:textSize="50dp" />

    <Button
    android:id="@+id/button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="Start" />

</RelativeLayout>

最佳答案

我看到的唯一方法是在您调用暂停时取消计时器,并在您想要恢复时创建一个新计时器,如下所示:

public class MainActivity extends ActionBarActivity implements OnClickListener {

 private MyCountDownTimer countDownTimer;
 private boolean timerHasStarted = false;
 private Button startB;
 public TextView text;
 private final long startTime = 30 * 1000;
 private final long interval = 1 * 1000;

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

    startB = (Button) this.findViewById(R.id.button);
    startB.setOnClickListener(this);

    text = (TextView) this.findViewById(R.id.timer);
    countDownTimer = new MyCountDownTimer(startTime, interval,text );
    // Set starting value
    text.setText(text.getText() + String.valueOf(startTime / 1000));
}

 public void onClick(View v) {
     if (!timerHasStarted) {
      // Start or resume counter
      countDownTimer = countDownTimer.resume();
      timerHasStarted = true;
      startB.setText("PAUSE");
      } else {
       // Pause counter
        countDownTimer.pause();
        timerHasStarted = false;
        startB.setText("RESTART");
      }
}

public class MyCountDownTimer extends CountDownTimer
{

    private long mRemainingTime = 0;
    private long mInterval = 0;
    private TextView mtvxToUpdate = null

    //  Use a textview reference to update text on each tick()
    public MyCountDownTimer(long startTime, long interval,TextView txv ) {
        super(startTime, interval);
        this.mInterval = interval;
        this.mRemainingTime = startTime;
        this.mtvxToUpdate = txv;
    }

    @Override
    public void onFinish() {
        mtvxToUpdate.setText("Time's up!");
    }

    @Override
    public void onTick(long millisUntilFinished) {
            mRemainingTime = millisUntilFinished;           
            mtvxToUpdate.setText("" + millisUntilFinished / 1000);
    }


    public void pause(){
        // Stop current timer 
        this.cancel();
    }

    public MyCountDownTimer resume(){
        // Create a counter with last saved data (just before pause)
        MyCountDownTimer newTimer = new MyCountDownTimer(mRemainingTime,mInterval,mtvxToUpdate);
        // Start this new timer that start where old one stop
        newTimer.start();
        // Return this new timer
        return newTimer;
    }
}
}

关于java - 如何在倒数计时器中实现暂停和恢复方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26669379/

相关文章:

c# - Unity C#如何使倒计时滴答声响起?

ios - 倒数计时器 ios 教程?

android - 在 AsyncTask 中使用 ProgressDialog 时应用程序崩溃

android : does new intent has "on Resume method"? new intent 能否自动关闭?

java - 什么可能导致 RecyclerView 中的计时器闪烁?

java - 在 javax.mail.MimeMessage 中设置发件人名称?

android - ListView getChildAt() 返回 null

java - 以最高安全设置运行java

java - Jdatechooser内容

java - Struts 2 中的多表单执行