如何将变量传递给postdelayed runnable?

时间:2011-10-23 16:42:37

标签: java runnable postdelayed

我创建了两个从runnable()实现的类。然后我用不同的延迟多次打电话给他们。问题是,当我稍后运行runnable时,它需要在调用postdelay时知道变量的值,而不是在它实际运行时。

我认为我现在需要做的是在调用post延迟时将变量传递给runnable实例。但对于我的生活,我无法弄清楚如何去做。以下是参考源代码:

public void PlaySequence() throws InterruptedException{

for(int i = 0;i<level;i++){
    mHandler.postDelayed(mToggleButtonOn, (i+1)*playbackOffTime);
    mHandler.postDelayed(mToggleButtonOff, (i+1)*playbackOnTime);           
    currentProgIteration++;
}        
currentProgIteration=0;     
}

private Runnable mToggleButtonOn = new Runnable() {

public void run() {
    try {
            if(Sequence[currentProgIteration]==0){
                ImageCard[0].setImageDrawable(getResources().getDrawable(R.drawable.yellow));
            }
            else if(Sequence[currentProgIteration]==1){
                ImageCard[1].setImageDrawable(getResources().getDrawable(R.drawable.blue));
            }
            else if(Sequence[currentProgIteration]==2){
                ImageCard[2].setImageDrawable(getResources().getDrawable(R.drawable.red));
            }
            else if(Sequence[currentProgIteration]==3){
                ImageCard[3].setImageDrawable(getResources().getDrawable(R.drawable.green));
            }                   
        }
     catch (IllegalStateException e) {
        e.printStackTrace();
    }

}
};

private Runnable mToggleButtonOff = new Runnable() {

public void run() {
    Log.d("Info", "running Off currentProgIteration: "+currentProgIteration);

    try {
            if(Sequence[currentProgIteration]==0){
                ImageCard[0].setImageDrawable(getResources().getDrawable(R.drawable.darkyellow));
            }
            else if(Sequence[currentProgIteration]==1){
                ImageCard[1].setImageDrawable(getResources().getDrawable(R.drawable.darkblue));
            }
            else if(Sequence[currentProgIteration]==2){
                ImageCard[2].setImageDrawable(getResources().getDrawable(R.drawable.darkred));
            }
            else if(Sequence[currentProgIteration]==3){
                ImageCard[3].setImageDrawable(getResources().getDrawable(R.drawable.darkgreen));
            }                   
        }
    catch (IllegalStateException e) {
        e.printStackTrace();
    }

}
};

runnables正在使用全局变量currentProgIteration,但是当它们在延迟之后运行时,该变量已被设置回0。

感谢您的任何建议。

2 个答案:

答案 0 :(得分:3)

最简单的方法可能是使用匿名的Runnable。

final int delay = (i+1)*playbackOffTime;
mHandler.postDelayed(new Runnable() {
   final Color[] colours = { R.drawable.yellow, R.drawable.blue, R.drawable.red, R.drawable.green } ;
   public void run() {
        try {
           // can use "delay" here
           int seq = Sequence[currentProgIteration];
           ImageCard[seq].setImageDrawable(getResources().getDrawable(colours[seq]));
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }
});

答案 1 :(得分:0)

正如彼得所说,您可以使用匿名 Runnable

要使 removeCallbacks(...) 成为可能,您可以使用令牌删除可运行对象:

为发布到 MessageQueue 的不同 Runnable 创建顶级令牌,例如

val animToken = Any()

为了跟踪您的 runnable,请使用以下扩展名:

fun Handler.postDelayedWithToken(token: Any, delayInMillis: Long, runnable: Runnable) {
    postAtTime(runnable, token, SystemClock.uptimeMillis() + delayInMillis)
}

示例使用: handler.postDelayedWithToken(animToken, 3000L, { hideSomething() })

为了处置(例如:在 onPause / onStop / onDestroyView 之前):

handler.removeCallbacksAndMessages(animToken)