首页 > 解决方案 > 为什么我的 CountDownTimer 似乎在倒退?

问题描述

我正在尝试制作一个应用程序,如果您的手机有一段时间不使用,它会提醒您为手机充电。它的工作原理是这样的:您输入手机在提醒您之前应该空闲多长时间。然后它会启动一个计时器并在完成时提醒您。

这是我的MainActivity.Java

public class MainActivity extends AppCompatActivity {
    //Defining UI elements
    public Button changeAppStateButton;
    public TextView minsEditText;

    //App variables
    boolean isAppRunning = false;
    public int secondsPhoneIsAsleep;

    public int timerDuration = secondsPhoneIsAsleep * 1000; //multiplying seconds by 100 to get milliseconds
    public int tickDuration = 60000; //multiplying seconds (1) by 100 to get milliseconds

    //Called when button is pressed
    public void changeAppState(View view) {
        Button changeAppStateButton = (Button) view;
        if (isAppRunning) { //If the app is running, stop app
            isAppRunning = false;
            changeAppStateButton.setBackgroundColor(getColor(R.color.colorPurple));
            changeAppStateButton.setText("Start Reminder");
            timer.cancel();
            Log.i("TIMER", "Timer interrupted");

        } else { //If the app is not running, start app
            secondsPhoneIsAsleep = Integer.parseInt(minsEditText.getText().toString()) * 60;
            isAppRunning = true;
            changeAppStateButton.setBackgroundColor(getColor(R.color.colorRed));
            changeAppStateButton.setText("Stop Reminder");
            timer.start();
            Log.i("TIMER", "Timer started");

        }
    }

    public CountDownTimer timer = new CountDownTimer(timerDuration, tickDuration) {
        @Override
        public void onTick(long millisUntilFinished) {
            Log.i("TIMER", "tick");
        }

        @Override
        public void onFinish() {
            isAppRunning = false;
            changeAppStateButton.setBackgroundColor(getColor(R.color.colorPurple));
            changeAppStateButton.setText("Start Reminder");
            Log.i("TIMER", "Timer finished");
        }
    };

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

        //Setting values for UI elements
        changeAppStateButton = findViewById(R.id.changeAppStateButton);
        minsEditText = findViewById(R.id.minEditText);

    }

这是我的 XML 的一部分:

    <EditText
        android:id="@+id/minEditText"
        android:layout_width="59dp"
        android:layout_height="42dp"
        android:layout_marginTop="14dp"
        android:ems="10"
        android:foregroundTint="#FF0000"
        android:inputType="number"
        android:text="30"
        android:textAlignment="center"
        android:textColor="#000000"
        android:textSize="20sp"
        app:layout_constraintStart_toEndOf="@+id/whenUntouched"
        app:layout_constraintTop_toBottomOf="@+id/numberEditText" />

    <Button
        android:id="@+id/changeAppStateButton"
        android:layout_width="0dp"
        android:layout_height="45dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="110dp"
        android:background="#9C27B0"
        android:fontFamily="@font/alegreya_sans_sc"
        android:onClick="changeAppState"
        android:text="Start Reminder"
        android:textAlignment="center"
        android:textColor="#FFFFFF"
        android:textSize="25sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent" />

当我按下按钮时,这是 logcat:

I/DEV_TIMER: Timer finished
I/DEV_TIMER: Timer started

为什么计时器似乎在倒退?为什么它不记录每个“滴答”的消息?
一些解释将不胜感激。我对 Android 和 Java 不太熟悉。

标签: javaandroid

解决方案


由于指定的代码MainActivity.java

  1. 不初始化 variable secondsPhoneIsAsleep,因此默认值为0.
  2. timerDuration也会如此0
  3. 所以timer被创建为计数持续时间0
  4. 因此,当单击按钮时,即使您读取了 的新值secondsPhoneIsAsleep,调用timer.start()也会导致它仅0根据先前的初始化值计数。
  5. 因此onFinish()被称为 logging Timer finished,然后Timer started被记录为按钮点击代码的一部分。

解决方案

如果您timer在单击按钮时创建实例,那么它应该使用正确的值secondsPhoneIsAsleep并正常工作。如下所示:

MainActvity.java

public class MainActivity extends AppCompatActivity {
    //Defining UI elements
    public Button changeAppStateButton;
    public TextView minsEditText;

    //App variables
    boolean isAppRunning = false;
    public int secondsPhoneIsAsleep;

    public CountDownTimer timer;

    public int timerDuration;
    public int tickDuration = 1000; //multiplying 1 second by 1000 to get milliseconds

    //Called when button is pressed
    public void changeAppState(View view) {
        Button changeAppStateButton = (Button) view;
        if (isAppRunning) { //If the app is running, stop app
            isAppRunning = false;
      changeAppStateButton.setBackgroundColor(getColor(R.color.colorPurple));
            changeAppStateButton.setText("Start Reminder");
            timer.cancel();
            Log.i("TIMER", "Timer interrupted");

        } else { //If the app is not running, start app
            secondsPhoneIsAsleep = Integer.parseInt(minsEditText.getText().toString()) * 60;
            timerDuration = secondsPhoneIsAsleep * 1000;
            timer = getNewTimer(); // Creates a new timer.
            isAppRunning = true;
         changeAppStateButton.setBackgroundColor(getColor(R.color.colorRed));
            changeAppStateButton.setText("Stop Reminder");
            timer.start();
            Log.i("TIMER", "Timer started");

        }
    }

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

        //Setting values for UI elements
        changeAppStateButton = findViewById(R.id.changeAppStateButton);
        minsEditText = findViewById(R.id.minEditText);

    }

    private CountdownTimer getNewTimer() {
      return new CountDownTimer(timerDuration, tickDuration) {
        @Override
        public void onTick(long millisUntilFinished) {
            Log.i("TIMER", "tick");
        }

        @Override
        public void onFinish() {
            isAppRunning = false;

changeAppStateButton.setBackgroundColor(getColor(R.color.colorPurple));
            changeAppStateButton.setText("Start Reminder");
            Log.i("TIMER", "Timer finished");
        }
    };
    }

推荐阅读