如何将变量传递到延迟后的可运行对象中?

发布于 2024-12-11 05:45:13 字数 2348 浏览 3 评论 0原文

我创建了两个从 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();
    }

}
};

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

感谢您的任何建议。

I have created two classes implemented from runnable(). I then call them several times, with varying delay. The problem is, when I run the runnable later, it needs to know the value of a variable back when the postdelay was called, not when it's actually run.

I think what I now need to do is pass the variable into the runnable instance when I call the post delay. But for the life of me I can't figure out how to do it. Here is the source code for reference:

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();
    }

}
};

The runnables are using global variable currentProgIteration, but by the time they run after the delay, that variable has been set back to 0.

Thanks for any advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜还是长夜 2024-12-18 05:45:13

最简单的方法可能是使用匿名 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();
        }
    }
});

The simplest approach may be to use an anonymous 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();
        }
    }
});
王权女流氓 2024-12-18 05:45:13

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

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

创建顶级令牌以将不同的可运行对象发布到 MessageQueue,例如

val animToken = Any()

为了跟踪您的可运行文件,请使用以下扩展:

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)

As Peter said, you can use anonymous Runnable

to make removeCallbacks(...) possible you could remove runnables with token:

create top level token to distinct Runnables posted onto MessageQueue, like

val animToken = Any()

in order to track your runnable, use following extension:

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

sample use:
handler.postDelayedWithToken(animToken, 3000L, { hideSomething() })

in order to dispose (e.g: before onPause / onStop / onDestroyView):

handler.removeCallbacksAndMessages(animToken)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文