1

嗨,在下面的代码中,我有一个开关按钮。没有选中/未选中的开关按钮想要每 15 分钟执行一次打开或关闭操作

使用 ischecked 它可以正常工作。但是如果不自动触摸该开关,应该会发生

谁能帮帮我吗

  geyserOnOff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

                    Log.d(TAG, "Checked once programatically :" + isProgrammatically);
                    if (!isProgrammatically) {

                        if (isChecked) {
                            /*geyser on method*/

                            geyserOnMethod();


                        } else {
                            if (ConstantUtils.REMAINING_DURATION_TIMER > 0) {
                                /*timer running alert dilaog*/
                                timerRunningAlertDialog();

                            } else if (isTimerRunning) {
                                 /*timer running alert dilaog*/

                                timerRunningAlertDialog();
                            } else {
                                /*geyser off method*/
                                long millisInfuture=15000;
                                long countDownInterval=1000;
                                new CountDownTimer(millisInfuture,countDownInterval){
                                    public void onTick(long millisUntilFinished){
                                        Toast.makeText(context,"Seconds Remaining:"+millisUntilFinished/1000,Toast.LENGTH_LONG).show();
                                    }
                                    public void onFinish(){
                                        Toast.makeText(context,"Time Over",Toast.LENGTH_LONG).show();
                                    }
                                }.start();
                                geyserOffMethod();

                            }


                        }
                    }
                    isProgrammatically = false;
                }
            });
4

1 回答 1

1

您可以继续使用 TimerTask!将计时器设置为 15 秒,并在执行中切换复选框并再次将计时器重置为 15 秒。

    Timer timer = new Timer();
    final TimerTask task = new TimerTask() {
           @Override
           public void run() {
               myCheckBox.setChecked(!myCheckBox.isChecked()); //Toggle
           }
       };
    timer.scheduleAtFixedRate(task, 15*1000, 15*1000); //Schedule from delay 15 seconds
   //schedule
于 2018-12-06T08:16:26.317 回答