java - 为什么我的 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 不太熟悉。
解决方案
由于指定的代码MainActivity.java
- 不初始化 variable
secondsPhoneIsAsleep
,因此默认值为0
. timerDuration
也会如此0
。- 所以
timer
被创建为计数持续时间0
。 - 因此,当单击按钮时,即使您读取了 的新值
secondsPhoneIsAsleep
,调用timer.start()
也会导致它仅0
根据先前的初始化值计数。 - 因此
onFinish()
被称为 loggingTimer 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");
}
};
}
推荐阅读
- html - 使用单击按钮进行网页抓取
- java - 为 POST 调用接收 HttpClientErrorException 时缺少 ResponseBodyAsString
- windows - 在 VMware 虚拟机上运行时如何获取当前的 Windows 操作系统版本?
- sql - 如何通过查看源中的另一列是否具有特定值来填充列,然后将其设置为单个值
- c# - 如何使用 Visual Studio 2019 创建 C# COM 对象
- python - 在上午 9 点到下午 6 点模拟客户类型
- c++ - 包含函数头的 C++ 给出错误 '__dest' 没有命名类型
- retrofit - 如何修复依赖解析错误
- python - 从数据框创建字典的字典
- python - 如何从 Python 脚本中检测 Android 操作系统?