【问题标题】:Variable delay before updating an android textview更新android textview之前的可变延迟
【发布时间】:2014-04-11 18:05:27
【问题描述】:

我已经连续 9 个小时尝试解决这个问题,但似乎没有任何效果。我正在构建一个应用程序,该应用程序在特定时间段内捕获 Android 手机中所有可用传感器的值,并将这些值存储在远程数据库中。 需要注意两点:

  1. 每个传感器都有自己的捕获频率(即加速度计每 10 秒,陀螺仪每 5 秒,接近度每 60 秒等...)。

  2. 此过程不会无限期地进行,因此有一个名为持续时间的变量,它指定捕获值所花费的总时间。例如,如果 acctime = 10 和 duration = 60,那么我们将在 0s、10s、20s、30s、40s、50s 和60 年代标记。

现在可以看代码了:

public void onSensorChanged(SensorEvent event) {
     Sensor s = event.sensor;
     if (s.getType() == Sensor.TYPE_ACCELEROMETER)
     {
          acc1 = event.values[0];
          acc2 = event.values[1];
          acc3 = event.values[2];
          accm = (float) Math.sqrt(acc1 * acc1 + acc2 * acc2 + acc3 * acc3);
     }
}

在上面的代码中,值已成功存储到各自的变量中。 接下来,我想在文本视图中显示这些变量,并按照之前指定的方式更新文本视图(根据频率和总持续时间):

public void capture(View view) {
        DecimalFormat df = new DecimalFormat("#.###");
        for (i = 0; i < duration; i++) {
            if ((i+1) % acctime == 0) {
                acc1 = Float.valueOf(df.format(acc1));  
                acc2 = Float.valueOf(df.format(acc2));
                acc3 = Float.valueOf(df.format(acc3));
                accm = Float.valueOf(df.format(accm));
                acctext.setText("i: " + i + "\nAccelerometer:\n\nX: " + acc1
                        + "\nY: " + acc2 + "\nZ: " + acc3 + "\nMagnitude: "
                        + accm);

            }
        }
    }

这里,测试if (i % acctime == 0) 保证每当 i 是 acctime 的倍数时更新 textview,这正是我们想要的:例如,如果 acctime = 10,则 textview 在 i = 0、10 时更新, 20、30、40、50 和 60。

但是,在我的一生中,我无法弄清楚如何在继续之前将循环暂停 1 秒(分配给 i 1 秒的时间值意味着循环有意义,从 0 到持续时间),所以剩下的就是将循环暂停 1 秒。

我在 try/catch 块之间尝试了Thread.Sleep(1000),但没有成功(它崩溃了)。

android.os.SystemClock.sleep(1000); 也是如此。

我还发现了一些看起来像

的代码
Handler handler = new Handler();
     handler.postDelayed(new Runnable() {
     public void run() {
      //insert code here
    }
}, 1000);

要么它不起作用,要么我执行不正确。 请,任何帮助将不胜感激。

以下是该功能的完整版本,它是对我来说最有意义的版本。请指出您能找到的任何错误:

public void capture(View view) {
        DecimalFormat df = new DecimalFormat("#.###");
        int i = 1;
        while (i <= duration) {
            if (acctime != -1 && (i % acctime == 0 || i == 1)) {
                acc1 = Float.valueOf(df.format(acc1));
                acc2 = Float.valueOf(df.format(acc2));
                acc3 = Float.valueOf(df.format(acc3));
                accm = Float.valueOf(df.format(accm));
                acctext.setText("Accelerometer:\n\nX: " + acc1 + "\nY: " + acc2
                        + "\nZ: " + acc3 + "\nMagnitude: " + accm);
            }
            if (magtime != -1 && (i % magtime == 0 || i == 1)) {
                mag1 = Float.valueOf(df.format(mag1));
                mag2 = Float.valueOf(df.format(mag2));
                mag3 = Float.valueOf(df.format(mag3));
                magm = Float.valueOf(df.format(magm));
                magtext.setText("Magnetometer\n\nX: " + mag1 + "\nY: " + mag2
                        + "\nZ: " + mag3 + "\nMagnitude: " + magm);
            }
            if (proxtime != -1 && (i % proxtime == 0 || i == 1)) {
                prox = Float.valueOf(df.format(prox));
                proxtext.setText("Proximity\n\nMagnitude: " + prox);
            }
            if (lighttime != -1 && (i % lighttime == 0 || i == 1)) {
                light = Float.valueOf(df.format(light));
                lighttext.setText("Light:\n\nMagnitude: " + light);
            }
            if (presstime != -1 && (i % presstime == 0 || i == 1)) {
                pressure = Float.valueOf(df.format(pressure));
                presstext.setText("Pressure:\n\nMagnitude: " + pressure);
            }
            if (gyrotime != -1 && (i % gyrotime == 0 || i == 1)) {
                gyro1 = Float.valueOf(df.format(gyro1));
                gyro2 = Float.valueOf(df.format(gyro2));
                gyro3 = Float.valueOf(df.format(gyro3));
                gyrom = Float.valueOf(df.format(gyrom));
                gyrotext.setText("Gyroscope:\n\nX: " + gyro1 + "\nY: " + gyro2
                        + "\nZ: " + gyro3 + "\nMagnitude: " + gyrom);
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            i++;
        }
    }

【问题讨论】:

  • 从崩溃中显示 logcat 错误?你是从 ui 线程处理 textview 吗?
  • 我正在从我当前正在使用/在屏幕上显示的活动的代码中处理文本视图。

标签: android time textview delay sensors


【解决方案1】:

如下申请Handler延迟1秒。

int i = 1;
public void capture(View view) {
    final DecimalFormat df = new DecimalFormat("#.###");


    Handler handler = new Handler();

    while (i <= duration) {

        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                if (acctime != -1 && (i % acctime == 0 || i == 1)) {
                    acc1 = Float.valueOf(df.format(acc1));
                    acc2 = Float.valueOf(df.format(acc2));
                    acc3 = Float.valueOf(df.format(acc3));
                    accm = Float.valueOf(df.format(accm));
                    acctext.setText("Accelerometer:\n\nX: " + acc1
                            + "\nY: " + acc2 + "\nZ: " + acc3
                            + "\nMagnitude: " + accm);
                }
                if (magtime != -1 && (i % magtime == 0 || i == 1)) {
                    mag1 = Float.valueOf(df.format(mag1));
                    mag2 = Float.valueOf(df.format(mag2));
                    mag3 = Float.valueOf(df.format(mag3));
                    magm = Float.valueOf(df.format(magm));
                    magtext.setText("Magnetometer\n\nX: " + mag1 + "\nY: "
                            + mag2 + "\nZ: " + mag3 + "\nMagnitude: "
                            + magm);
                }
                if (proxtime != -1 && (i % proxtime == 0 || i == 1)) {
                    prox = Float.valueOf(df.format(prox));
                    proxtext.setText("Proximity\n\nMagnitude: " + prox);
                }
                if (lighttime != -1 && (i % lighttime == 0 || i == 1)) {
                    light = Float.valueOf(df.format(light));
                    lighttext.setText("Light:\n\nMagnitude: " + light);
                }
                if (presstime != -1 && (i % presstime == 0 || i == 1)) {
                    pressure = Float.valueOf(df.format(pressure));
                    presstext
                            .setText("Pressure:\n\nMagnitude: " + pressure);
                }
                if (gyrotime != -1 && (i % gyrotime == 0 || i == 1)) {
                    gyro1 = Float.valueOf(df.format(gyro1));
                    gyro2 = Float.valueOf(df.format(gyro2));
                    gyro3 = Float.valueOf(df.format(gyro3));
                    gyrom = Float.valueOf(df.format(gyrom));
                    gyrotext.setText("Gyroscope:\n\nX: " + gyro1 + "\nY: "
                            + gyro2 + "\nZ: " + gyro3 + "\nMagnitude: "
                            + gyrom);
                }

                i++;
            }

        }, 1000);
    }
}

【讨论】:

  • 感谢您的回复。但是,我运行了您的代码,应用程序冻结了大约 1 分钟(持续时间为 30 秒),然后它崩溃了。为了查明问题,我可以为您提供什么?
  • 请任何帮助将不胜感激...我有点卡住了:S
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-19
  • 1970-01-01
  • 1970-01-01
  • 2021-01-31
  • 2013-02-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多