简体   繁体   English

嵌套的postDelayed / Runnable / Handler Android

[英]nested postDelayed / Runnable / Handler Android

I am trying to use a nested postDelayed because I need to do something after (delayed for) 5 minutes, stop it after (delayed) 30 seconds, do something else, then repeat both events in the cycle again from the start. 我试图使用嵌套的postDelayed,因为我需要在(延迟)5分钟之后做一些事情,在延迟30秒后停止它,做其他事情,然后从开始再次重复循环中的两个事件。 I just can't seem to get it right. 我似乎无法做对。

code I have sofar: 代码我有sofar:

private long EnabledAfter  = 300000; // 5 minutes
private long DisabledAfter = 30000;  // 30 seconds

public void start_timers(){
    on_delayed(EnabledAfter);
}//end method

private void on_delayed(long period_off){       
    Delayed = new Runnable() {
        public void run() {                                                     
            something.enable(context);                          
            something.enable_else(context, true);       
            off_delayed(DisabledAfter); // and disable both again delayed

            Handler.postDelayed(Delayed, EnabledAfter);
        }
    };
    Handler.postDelayed(Delayed, EnabledAfter);
}//end method

private void off_delayed(long period_on){       
    Delayed = new Runnable() {
        public void run() {
            something.disable(context);                                 
            something.disable_else(context, false); 
            on_delayed(period_on); // start the proces again from the start...

            //Handler.postDelayed(Delayed, DisabledAfter);              
        }
    };
    Handler.postDelayed(Delayed, period_on);
}//end method

The problem with this is runs fine the first run, but then seems to stack on top of each other...and all delays are borked. 这个问题在第一次运行时运行良好,但后来似乎堆叠在一起...并且所有延迟都被堵塞了。 I need to execute the both Runnable s in exactly 5 minutes and 30 seconds, then repeat the process. 我需要在5分30秒内执行两个Runnable ,然后重复该过程。

The net result after this code has run a few times is that the Handler posts way too many instances of each Runnable . 此代码运行几次后的最终结果是Handler发布了每个Runnable太多实例。 As written above: 如上所述:

  1. First on_delayed posts 1 Runnable 首先on_delayed帖子1 Runnable
  2. That runnable fires and then posts 2 Runnables (one in off_delayed, and another before returning from run()). 该runnable会触发,然后发布2个Runnables(一个在off_delayed中,另一个在从run()返回之前)。
  3. This will continue to multiply because when those two Runnables fire, 4 will get created, an so on. 这将继续增加,因为当这两个Runnables触发时,将创建4,依此类推。

You are also not taking advantage of the fact that a Runnable can be posted to the same queue multiple times, it doesn't have to be created new each time. 您也没有利用Runnable可以多次发布到同一队列的事实,不必每次都创建新的。 This is essential if you want to cancel the actions, because the remove method on Handler look for all the matching instances to remove from the queue. 如果要取消操作,这是必不可少的,因为Handler上的remove方法会查找要从队列中删除的所有匹配实例。 You might try something like this instead: 您可以尝试这样的事情:

private long EnabledAfter  = 300000; // 5 minutes
private long DisabledAfter = 30000;  // 30 seconds

private Runnable Enabler = new Runnable() {
    public void run() {                                                     
        something.enable(context);                          
        something.enable_else(context, true);       

        Handler.postDelayed(Disabler, DisabledAfter);
    }
};

private Runnable Disabler = new Runnable() {
    public void run() {
        something.disable(context);                                 
        something.disable_else(context, false); 

        Handler.postDelayed(Enabler, EnabledAfter);              
    }
};

public void start_timers(){
    Handler.postDelayed(Enabler, EnabledAfter);
}//end method

public void stop_timers(){
   Handler.removeCallbacks(Enabler);
   Handler.removeCallbacks(Disabler);
}//end method

I also added one more method you can use to cancel the timer operation by removing all the instances of your Runnable items from the queue. 我还添加了一个方法,您可以通过从队列中删除Runnable项的所有实例来取消计时器操作。

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM