如何每15秒自动打开一个开关按钮on/off?

How to automatically turn a switch button on/off every 15 seconds?

你好,在下面的代码中我有一个开关按钮。没有 checked/unchecked 开关按钮想要每 15 分钟进行一次打开或关闭操作

有了 ischecked working.but 想要在不接触的情况下自动切换

谁能帮帮我

  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;
                }
            });

您可以继续使用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