java - 如何在android studio中停止计时器,然后从停止时重新开始?

标签 java android android-studio timer

我正在制作一个番茄计时器应用程序。我已经实现了播放和停止功能。现在我正在尝试实现暂停功能。当我按下暂停按钮时,它成功暂停了计时器,但是,当我再次按下播放按钮时,它从 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** **

最佳答案

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

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

关于java - 如何在android studio中停止计时器,然后从停止时重新开始?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59991586/

相关文章:

java - 如何修复adb : error: cannot bind listener: Operation not permitted react-native?

java - 异常 cvc-elt.1 : Cannot find the declaration of element 'AsifXml' when trying to unmarshall using JAXB

java - 将 native C++ 代码转换为 Java

java - FileProvider.getUriForFile 返回空对象引用

android - 具有用于不同gradle任务的不同Android lintOptions

java - 在 Android 中实现单用户 OAuth

android - 属于项目类的 BadTokenException

java - Gradle同步失败: Cause: unable to find valid certification path to requested target on MacOS

java - 如何在 Java 中使单选按钮动态更改文本

android - 如何在易受攻击的 Intent 方案劫持中为 WebView 使用正确的 Intent