所以我试图分析传感器的一些数据。我想让数组列表“填满”数据,然后在500ms后,线程消失并处理它。看起来这个线程只执行一次(我知道这是应该发生的事情),但我无法想出一个方法来执行它多次,它睡了500毫秒。我尝试使用while(true)
,lock
,handler
,timer
并没有得到任何好处。执行线程onSensorChanged
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
accSensorVals = lowPass(sensorEvent.values.clone(), accSensorVals);
}
xAxisValues.add(accSensorVals[0]); // [0] - X axis
yAxisValues.add(accSensorVals[1]); // [1] - Y axis
zAxisValues.add(accSensorVals[2]); // [2] - Z axis
new Thread() {
public void run() {
try {
Thread.sleep(DELAY); // delay for 500 ms
checkSpikes(xAxisValues, yAxisValues, zAxisValues);
xAxisValues = new ArrayList<>();
yAxisValues = new ArrayList<>();
zAxisValues = new ArrayList<>();
} catch (Exception e) {}
}
}.start();
}
我使用这段代码在后台服务已经,并使用while循环消耗CPU周期的很多,所以我不认为会是最佳解决方案。但是,您的解决方案似乎仍然无法正常工作。不过谢谢你。 –
然后检查此无限循环,https://developer.android.com/reference/android/app/AlarmManager.html –