Android handler.postDelayed() 每次迭代都会加速

Android handler.postDelayed() speeds up with every iteration

我有这样的处理程序:

    public void changeSpinnerWaitSelection2() {
    Runnable runnable = new Runnable()
    {
        public void run()
        {
            if (count <= 3) spinnerWait.setSelection(count, true);
            count++;
            mTimerHandler2.postDelayed(this, 500);
        }
    };
    runnable.run();
   }

每次迭代都会加快速度。变量 count 在另一个地方被重置。这个处理程序有什么问题?

而另一种方法完全相同:

public void changeSpinnerWaitSelection() {
    TimerTask mTt1 = new TimerTask() {
        public void run() {
            mTimerHandler5.post(() -> {
                if (count > 4) { mTimer2.cancel();}
                if (count <= 3) spinnerWait.setSelection(count, true);
                count++;
            });
        }
    };
    mTimer2 = new Timer();
    mTimer2.scheduleAtFixedRate(mTt1, 0, 500);
}

但我更喜欢第一个版本,因为缺少 TimerTask,这似乎对我的一个设备有害。

第一个变体还没有退出并且运行无限且可能重复

尝试

if (count <= 3) spinnerWait.setSelection(count, true);
            count++;
            mTimerHandler2.postDelayed(this, 500);

替换为

if (count <= 3) {
            spinnerWait.setSelection(count, true);
            count++;
            mTimerHandler2.postDelayed(this, 500);
}