使用定时器的占空比执行周期



我需要执行占空比功能,以便在某些时段内使用级联的定时器运行两个操作,以便在使用定时器1运行操作1的ON时段(x秒)期间,以及当定时器1完成时,接着是使用定时器2运行操作2的第二个off时段(y秒),并很快重复该场景。我是的初学者

请谁帮我好好跑。

我试着写下面的代码,它看起来像:

 package com.example.periodictimer;

公共类MainActivity扩展Activity{

Timer t1 = new Timer();
Timer t2 = new Timer();
TimerTask mTimerTask1;
TimerTask mTimerTask2;
TextView tv1;
TextView tv2;
boolean z;
Handler hand = new Handler();
Handler hand1 = new Handler();
Button hButtonStart;
int time =0;
int time1 =0;
boolean flag1;

@Overrideprotected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
doTimerTask1();

}public void doTimerTask1(){

mTimerTask1 = new TimerTask() {
    public void run() {
        hand.post(new Runnable() {
            public void run() {
                time++;
                tv1.setText("Execute Operation1: " + time);
                doTimerTask2();
            }
        });
    }
    };
    // public void schedule (TimerTask task, long delay, long period) 
    t1.schedule(mTimerTask1,0, 3000);  // 

}

public void doTimerTask2(){

mTimerTask1 = new TimerTask() {
    public void run() {
        hand.post(new Runnable() {
            public void run() {
                time1++;
                // update TextView
                tv2.setText("Execute Operation2:" + time1);
                //Log.d("TIMER", "TimerTask run");
            }
        });
    }};
    // public void schedule (TimerTask task, long delay, long period) 
    t1.schedule(mTimerTask2,500, 5000);  // 

}

}

我建议您使用两个定时器和scheduleAtFixedRate方法,而不是schedule方法。

这种方法的工作原理如下scheduleAtFixedRate(timerTask, delay, period)

其中:TimerTask:要执行的任务是TimerTask类的实例。在这里,你可以在一个定时器任务中打开你的标志,在另一个任务中关闭它。

延迟:第一次运行任务之前的延迟量。

period:连续执行定时器任务之前的占空比周期。

诀窍是调度两个具有相同延迟周期的定时器,但其中一个以0延迟开始,另一个以延迟=ON_period开始。

下面的代码示例在一个java程序中显示了它,其中一个标志打开4秒,然后关闭2秒,依此类推

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Timer;
import java.util.TimerTask;

public class TestTimer {
    TimerTask timerTask2;
    TimerTask timerTask1;
    Timer t1 = new Timer();
    Timer t2 = new Timer();
    boolean flag = true;
    private Date date;
    private DateFormat  dateFormat;
    public TestTimer() {
        flag = true;
        dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");;
    }
    public void test() {
        timerTask1 = new TimerTask() {
            @Override
            public void run() {
                flag = true;
                date = new Date();
                System.out.println("Task1  [" + (flag ? "ON" : "OFF" ) + "] " +
                dateFormat.format(date));
            }
        };
        timerTask2 = new TimerTask() {
            @Override
            public void run() {
                flag = false;
                date = new Date();
                System.out.println("Task2  [" + (flag ? "ON" : "OFF" ) + "] " +
                        dateFormat.format(date));
            }
        };

        t1.scheduleAtFixedRate(timerTask1, 0, 6000);
        t2.scheduleAtFixedRate(timerTask2, 4000, 6000);

    }
    public static void main(String [] args) {

        TestTimer tt = new TestTimer();
        tt.test();
    }
}

最新更新