本文介绍了以编程方式减少处理程序中的时间间隔.安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在处理程序上使用postDelayed函数时,需要delayMillis变量来指定处理程序运行的时间.我希望处理程序无限期地重复,因此我嵌套了两个postDelayed函数.

When you use the postDelayed function on a handler, a delayMillis variable is required to specify the time after which the handler runs. I want my handler to repeat indefinitely so I have nested two postDelayed functions.

    final int initialdt = 1000;
    final Handler handler = new Handler();
    final Runnable r = new Runnable() {

        public void run() {

            handler.postDelayed(this, initialdt);
        }
    };
    handler.postDelayed(r, initialdt);

但是,使用此方法时,run()调用之间的时间是固定的.由于内部postDelayed需要 final 整数作为参数.我想减少连续通话之间的时间.有办法吗?

However using this method, the time between the run() calls is fixed. Since the inner postDelayed requires a final integer as a parameter. I want to reduce the time between consecutive calls. Is there a way to do so?

推荐答案

这应该做您想要的.

    final int initialDt = 1000;
    final Handler handler = new Handler();
    final Runnable r = new Runnable() {
        int dt = initialDt;
        public void run() {
            dt -= 100;
            if (dt >= 0) {
                handler.postDelayed(this, dt);
            }
        }
    };
    handler.postDelayed(r, initialDt);

这篇关于以编程方式减少处理程序中的时间间隔.安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 15:02