嵌套postDelayed / Runnable接口/处理器的Andr​​oid [英] nested postDelayed / Runnable / Handler Android

查看:265
本文介绍了嵌套postDelayed / Runnable接口/处理器的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个嵌套postDelayed,因为我需要做的后,一些(延迟)5分钟,(延迟)30秒后停止,去做别的事情,然后在循环再次从一开始就重复这两个事件。 我似乎无法得到它的权利。

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. I just can't seem to get it right.

code我有SOFAR:

code I have 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

这个是问题运行良好的第一次运行,但随后似乎堆叠在彼此顶部...和所有延迟borked。我需要执行两个的Runnable S在精确至5分30秒,然后重复上述过程。

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 Runnables in exactly 5 minutes and 30 seconds, then repeat the process.

推荐答案

在此之后code的最终结果已经跑了几次的是,处理程序帖子的方式每个太多的情况下,的Runnable 。正如上面写的:

The net result after this code has run a few times is that the Handler posts way too many instances of each Runnable. As written above:

  1. 在第一on_delayed职位1的Runnable
  2. 在该Runnable火灾再上岗2的Runnable(一个在off_delayed,另一个是运行()返回前)。
  3. 这也将继续增加,因为这两个的Runnable火,4将获得创建时,一个等等。

您还没有利用这样一个事实:的Runnable 可以发布到同一个队列多次的优势,它没有新的每次创建。如果要取消操作,这是必要的,因为在处理程序remove方法查找所有匹配的情况下,从队列中删除。您可以尝试这样的事情,而不是:

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. 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. 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

我还添加了一个你可以使用从队列中移除你的的Runnable 项的所有实例取消计时器操作的方法。

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.

心连心

这篇关于嵌套postDelayed / Runnable接口/处理器的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆