【问题标题】:How to stop a timer in android studio and then start again from when it was stopped?如何在android studio中停止计时器,然后从停止时重新开始?
【发布时间】:2020-01-30 17:39:56
【问题描述】:

我正在制作番茄钟应用程序。我已经实现了播放和停止功能。现在我正在尝试实现暂停功能。当我按下暂停按钮时,它成功地暂停了计时器,但是,当我再次按下播放按钮时,它从 25:00 重新开始,而不是从我停止的时间开始。如何实现此方法?

代码如下:

package com.example.treeplanting;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import java.util.concurrent.TimeUnit;

@SuppressWarnings("unused")
public class Pomodoro extends AppCompatActivity {

    private TextView tvTimer, tvMotivationalQuote;
    private ImageView ivStop, ivPause, ivPlay;
    private SeekBar seekBar;

    private CountDownTimer countDownTimer;
    private boolean isRest = false;
    private int userSelectedDuration = -1;

    private final int ONE_SECOND_TICK = 1000;
    private int counter = 0;

    MediaPlayer mediaPlayer;

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

        tvTimer = findViewById(R.id.tvTimer);
        tvMotivationalQuote = findViewById(R.id.tvMotivationalQuote);
        ivPlay = findViewById(R.id.ivPlay);
        ivStop = findViewById(R.id.ivStop);
        ivPause = findViewById(R.id.ivPause);

        seekBar = findViewById(R.id.seekBar);

        tvMotivationalQuote.setText(MethodsContainer.getMotivationalQuote());

        // Setting up Seek Bar

        seekBar.setMax(2500);
        seekBar.setProgress(1500);
        seekBar.setVisibility(View.GONE);

        tvTimer.setText("25:00");
    }

    // On click methods

    public void startTimer(View v){
        ivPlay.setClickable(false);

        isRest = false;
        int duration = seekBar.getProgress();
        userSelectedDuration = duration;
        restartTimer(duration);

        ivStop.setClickable(true);
    }

    public void stopTimer(View v){
        ivPlay.setClickable(true);

        seekBar.setProgress(1500);
        tvTimer.setText("25:00");

        ivStop.setClickable(false);
    }

    public void pauseTimer(View v){
        if(countDownTimer != null) {
            countDownTimer.cancel();
        }

        ivPlay.setClickable(true);
    }

    // Update methods

    private void updateTimer(int progress){
        int minutes = progress / 60;
        int seconds = progress - minutes * 60;

        String textMinutes = String.valueOf(minutes);
        String textSeconds = String.valueOf(seconds);

        if(seconds < 10) textSeconds = "0" + textSeconds;
        if(minutes < 10) textMinutes = "0" + textMinutes;

        tvTimer.setText(textMinutes + ":" + textSeconds);
    }

    private void restartTimer(int duration){
        countDownTimer = new CountDownTimer(TimeUnit.SECONDS.toMillis(duration) + 100, ONE_SECOND_TICK) {
            @Override
            public void onTick(long millisUntilFinished) {
                updateTimer((int) millisUntilFinished / 1000);
            }

            @Override
            public void onFinish() {
                breakTimer();

                counter = counter + 1;

                mediaPlayer = MediaPlayer.create(Pomodoro.this, R.raw.alarm);
                mediaPlayer.start();
            }
        };

        countDownTimer.start();
    }

    private void breakTimer(){
        isRest = !isRest;


        if(isRest){
            seekBar.setProgress(300);
            tvTimer.setText("5:00");
            restartTimer(300);
        } else{
            restartTimer(userSelectedDuration);
        }
    }
}

****输出:25:00 -> 24:05(我按下暂停按钮,它停止...) -> 我按下播放按钮 -> 24:59 预期输出:25:00 -> 24:05(我按下暂停按钮,它停止...) -> 我按下播放按钮 -> 24:04** **

【问题讨论】:

  • 您想将时间存储在哪个变量中?似乎您正在使用 countDownTimer 变量来执行此操作,但您在 pauseTimer 中将其设置为 null。
  • 我删除了 countDownTimer = null;
  • 请在您的帖子中也进行更改,并确认该应用仍然无法正常运行。
  • 完成。请帮帮我
  • 您正在使用 countDownTimer 跟踪剩余的时间量,但每次重新启动计时器时都会实例化一个新的 CountDownTimer 类。

标签: java android android-studio timer


【解决方案1】:

查看此信息以获取有关开发倒计时应用程序的帮助:

https://github.com/harish124/Android_Data_Binding/tree/master/AlarmClock

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-04
    • 2012-05-05
    • 2012-05-29
    • 1970-01-01
    相关资源
    最近更新 更多