【问题标题】:Running multiple Runnables with Handler.postDelayed使用 Handler.postDelayed 运行多个 Runnable
【发布时间】:2012-10-29 18:24:40
【问题描述】:

我正在尝试让一个 sn-p 代码定期运行。
这是我的代码:

int endTime = 52;
final double[] weights = new double[endTime];
for (int j = 0; j < endTime; j++) {
    final int k = j;
    newWeight = i.integrate(getCarbs(), getProt(), getFat(), newWeight,
            height, age, PAL, gender, 7);
    double percentChange = (newWeight - weight);
    percentChange = percentChange * 100 / weight;
    if(percentChange <-100){
        percentChange = -100;
    }
    weights[j] = percentChange;
    final DecimalFormat twoDForm = new DecimalFormat("0.00");
    final Handler h = new Handler();
    int time = 300*(j);
    Runnable r  = new Runnable() {
        public void run() {
            ((TextView) findViewById(R.id.weightGainNumbers))
                    .setText("Week:\t" + (k + 1) + "\nWeight Change:\t"
                        + twoDForm.format(weights[k]) + "%");
            animate(weights[Math.abs(k - 1)], weights[k], false);
        }
    };
    h.postDelayed(r, time);
}

动画只需要 100 毫秒。但是,当我运行它时,应用程序会挂起,并且只会在 j = 15 左右开始执行它应该做的事情。有人知道这里出了什么问题吗?

【问题讨论】:

  • 您在 Activity 的哪个位置执行此操作?
  • 如果你只声明一个 Handler 并在你的 for 循环之外执行它会有什么不同吗?
  • 它在 onClick 上调用,将处理程序移到外面没有任何帮助
  • 如果我们使用 handler.postDelayed(runnableName, 100);使用不同的可运行对象多次声明可以吗?

标签: android handler runnable


【解决方案1】:

您在循环的每次迭代中都在执行不必要的工作,例如创建新的 DecimalFormats,而您可以简单地重用一个。此外,您只需要一个处理程序,每个视图都已经有一个处理程序。所有这些都应该运行得更顺利。

首先,设置一些类变量:

final DecimalFormat twoDForm = new DecimalFormat("0.00");
TextView weightGainNumbers;
int weightGainIndex = 0;
final double[] weights;

Runnable r  = new Runnable() {
    public void run() {
        weightGainNumbers.setText("Week:\t" + (weightGainIndex + 1) + "\nWeight Change:\t"
                    + twoDForm.format(weights[weightGainIndex]) + "%");

        if(weightGainIndex > 0)
            animate(weights[Math.abs(weightGainIndex - 1)], weights[weightGainIndex], false);
        // This animation is a guess, but you get the idea...
        else
            animate(0, weights[weightGainIndex], false);

        weightGainIndex++;
        // Call the next animation or reset the index for next time
        if(weightGainIndex < weights.length)
            weightGainNumbers.postDelayed(r, 300);
        else
            weightGainIndex = 0;
    }
};

接下来,初始化onCreate()中的weightGainNumbersTextView。

最后,使用这个:

int endTime = 52;
weights = new double[endTime];
for (int j = 0; j < endTime; j++) {
    newWeight = i.integrate(getCarbs(), getProt(), getFat(), newWeight,
            height, age, PAL, gender, 7);
    weights[j] = Math.max(percentChange * 100 / (newWeight - weight), -100);
}
weightGainNumbers.post(r);

如果您有任何具体问题,请告诉我。

【讨论】:

  • 如果runnable是类变量,什么时候调用?
  • 哎呀,我现在修好了。让我知道其他任何错误,因为我自己无法真正运行它。
  • 如果我们使用 handler.postDelayed(runnableName, 100);使用不同的可运行文件多次声明可以吗?
猜你喜欢
  • 2016-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多