Java Android handler.postdayed()会随着每次迭代而加速
我有这样的经历:Java Android handler.postdayed()会随着每次迭代而加速,java,android,android-handler,Java,Android,Android Handler,我有这样的经历: public void changeSpinnerWaitSelection2() { Runnable runnable = new Runnable() { public void run() { if (count <= 3) spinnerWait.setSelection(count, true); count++; mTimerHan
public void changeSpinnerWaitSelection2() {
Runnable runnable = new Runnable()
{
public void run()
{
if (count <= 3) spinnerWait.setSelection(count, true);
count++;
mTimerHandler2.postDelayed(this, 500);
}
};
runnable.run();
}
但我更喜欢第一个版本,因为缺少TimerTask,这似乎对我的一台设备有害。第一个版本没有退出并无限运行,可能会重复 试一试
if(count第一个变量未退出并无限运行,可能重复
试一试
if(计数我建议使用Thread.sleep而不是TimerHandlerWith Thread sleep它根本不起作用我的意思是没有调用此微调器选择。然后不要在主UI上调用此方法Thread declare asycn方法,因为这样做很容易。我建议使用Thread.sleep而不是TimerHandlerWith Thread sleep它根本不起作用我的意思是这个微调器选择没有被调用。那么不要在主UI线程上调用这个方法,因为它将很容易在那里完成
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);
}
if (count <= 3) spinnerWait.setSelection(count, true);
count++;
mTimerHandler2.postDelayed(this, 500);
if (count <= 3) {
spinnerWait.setSelection(count, true);
count++;
mTimerHandler2.postDelayed(this, 500);
}