我使用智能手机从外接套件接收加速度值。我的主要目标是检测跌倒,并且我使用以下算法来实现此目的:
// Method to be triggered, when data is received over BLE interface
public void onCharacteristicChanged(BluetoothGattCharacteristic c) {
data = parse(c);
}
public T getData() {
return data;
}
@Override
public String getDataString() {
final float[] data = getData();
return TiSensorUtils.coordinatesToString(data);
}
@Override
public float[] parse(final BluetoothGattCharacteristic c) {
Integer x = c.getIntValue(FORMAT_SINT8, 0);
Integer y = c.getIntValue(FORMAT_SINT8, 1);
Integer z = c.getIntValue(FORMAT_SINT8, 2);
double X = x / 64.0;
double Y = y / 64.0;
double Z = -1 * z / 64.0;
double G = Math.sqrt((X * X) + (Y * Y) + (Z * Z));
double L = 1;
if ((G > 1.3 || G < 0.7) && timerStatus.equals("OFF"))
{
// I WANT THE DELAY HERE !!!!!!!!
if ((Z < -0.9 && Z > -1.1 || (Z < 1.1 && Z > 0.9)) && (X < 0.3 && X > -0.3) && (Y < 0.3 && Y > -0.3))
{
cdt = new accCountDownTimer(4000, 1000);
cdt.start();
timerStatus = "ON";
return new float[]{(float)X, (float)Y, (float)Z, (float)G, (float)L};
}
}
return new float[]{(float)X, (float)Y, (float)Z, (float)G};
}
我想在第一个&#34; if&#34;之后找到一种任务延迟功能。声明,但由于我不想阻止该线程,因此在这种情况下,Thread.Sleep(2000)将毫无用处。我已成功在C#中实现了相应的算法,我在其中使用了Task.delay(2000)。 java中有类似的方法吗?
答案 0 :(得分:2)
snoozeTimer.
schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
onSnoozeTimerExpiry();
}
});
}
}, snoozeDuration);
答案 1 :(得分:1)
使用
new Handler().postDelayed(Runnable, delaytime);
更新回答:
您可能想要这样做
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
data = parse(c);
}
}, delayMillis);