如何每15秒自动打开/关闭一个开关按钮



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

选中后,它正在工作。但如果不触摸开关,就会自动发生

有人能帮我吗

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

最新更新