Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的倒计时好像在倒计时?_Java_Android - Fatal编程技术网

Java 为什么我的倒计时好像在倒计时?

Java 为什么我的倒计时好像在倒计时?,java,android,Java,Android,我正在尝试制作一个应用程序,如果手机暂时不用,它会提醒你给手机充电。它的工作原理是这样的:你输入手机在提醒你之前应该空闲多长时间。然后它启动计时器,并在结束时提醒您 下面是我的MainActivity.Java: public class MainActivity extends AppCompatActivity { //Defining UI elements public Button changeAppStateButton; public TextView mi

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

下面是我的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" />
为什么计时器似乎在倒计时?为什么它不每“滴答”一次就记录一条消息?

如能作出解释,将不胜感激。我对Android和Java没有太多经验。

因为在
MainActivity.Java

  • 不初始化变量
    secondsphoneissleep
    ,因此默认值为
    0
  • 因此,
    timerDuration
    将是
    0
  • 因此,将创建
    计时器
    ,以在
    0
    的持续时间内计数
  • 因此,当单击该按钮时,即使您读取了
    secondsphoneissleep
    的新值,调用
    timer.start()
    也将使其根据先前初始化的值仅计数到
    0
  • 因此,
    onFinish()
    被调用为记录
    计时器完成
    ,然后
    计时器启动
    被记录为按钮单击代码的一部分
  • 解决方案

    如果在单击按钮时创建
    timer
    实例,则它应使用正确的
    secondsphoneissleep
    值并正常工作。如下图所示:

    maintactivity.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");
            }
        };
        }
    

    你对tickDuration的评论似乎暗示一秒钟内有100毫秒而不是1000毫秒。滴答声多长时间发生一次?从你的代码中,我希望它每60000ms=60s=1minCan打勾一次。你发布了完整的
    MainActivity.java
    文件吗?@VivekSasidharan我发布了几乎所有的东西,除了onCreate(),但没问题。@ContinuousLoad-这是评论中的一个错误,抱歉。我现在用1000乘一次code@AmethystAurora请看我的答案。我确实初始化了
    secondsphoneissleep
    ——就在
    else
    之后的一行。“我是不是误解了什么?”紫水晶极光是的,在点击按钮的过程中。但是到那时,当您使用
    secondsPhoneIsAsleep
    的初始值调用
    new 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 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");
            }
        };
        }