我正在尝试编写一种方法,该方法在特定时间段内收集加速度计传感器值,并返回该时间段内传感器读数的平均值。
它应该是一个同步的阻塞方法,一旦被调用将阻塞调用线程一段时间然后将返回传感器平均值
我确实检查了以下类似的问题,但似乎没有适合我的案例的正确解决方法:
SensorEventListener in separate thread
Android - how to run your sensor ( service, thread, activity )?
A method for waiting for sensor data
我还尝试使用与this question类似的Executors
,但无法按照我的意愿使用它。
下面是我的代码框架,其中方法sensorAverage
是一种阻塞方法,用于计算加速度计传感器在一段时间内的平均值等于timeout
参数
Average average = new Average(); // Some class to calculate the mean
double sensorAverage(long timeout){
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
sensorManager.registerListener(this, sensor,SensorManager.SENSOR_DELAY_NORMAL);
// This does not work
Thread.sleep(timeout);
sensorManager.unregisterListener(this);
return average.value();
}
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION) {
double x2 = Math.pow(event.values[0], 2);
double y2 = Math.pow(event.values[1], 2);
double z2 = Math.pow(event.values[2], 2);
average.add(Math.sqrt((x2 + y2 + z2)));
}
}
修改:
我知道我需要另一个线程,但是我需要运行它一段特定时间的问题,到目前为止我找不到合适的工作解决方案。因为当我使用另一个线程时,我得到的传感器平均值始终为0
答案 0 :(得分:4)
我设法实现了一个完全符合我想要的解决方案。
一种阻塞方法,用于收集特定时期的传感器值,并返回所有传感器读数的统计数据,即均值和方差。
可以简单地存储所有传感器的值,然后计算均值和方差;但是如果在长时间内收集高频传感器,可能会耗尽内存。
我找到了一个更好的解决方案,可以使用下面的RunningStat
类来实时计算数据流的均值和方差(即不存储传感器值)
示例代码:
// Calculate statistics of accelerometer values over 300 ms (a blocking method)
RunningStat[] stats = SensorUtils.sensorStats(context,
Sensor.TYPE_ACCELEROMETER, 300)
double xMean = stats[0].mean();
double xVar = stats[0].variance();
完整课程代码:
public class SensorUtils {
// Collect sensors data for specific period and return statistics of
// sensor values e.g. mean and variance for x, y and z-axis
public static RunningStat[] sensorStats(Context context, int sensorType,
long timeout) throws Exception {
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<RunningStat[]> future = executor.submit(new SensorTask(context,
sensorType, timeout));
RunningStat[] stats = future.get();
return stats;
}
private static class SensorTask implements Callable<RunningStat[]> {
private final Context context;
private final long timeout;
private final int sensorType;
// We need a dedicated handler for the onSensorChanged
HandlerThread handler = new HandlerThread("SensorHandlerThread");
public SensorTask(Context context, int sensorType, long timeout) {
this.context = context;
this.timeout = timeout;
this.sensorType = sensorType;
}
@Override
public RunningStat[] call() throws Exception {
final SensorCollector collector = new SensorCollector(context);
handler.start();
Thread sensorThread = new Thread() {
public void run() {
collector.start(sensorType,
new Handler(handler.getLooper()));
};
};
sensorThread.start();
Thread.sleep(timeout);
return collector.finishWithResult();
}
}
private static class SensorCollector implements SensorEventListener {
protected Context context;
protected RunningStat[] runningStat;
protected SensorManager sensorManager;
protected int sensorType;
public SensorCollector(Context context) {
this.context = context;
}
protected void start(int sensorType, Handler handle) {
if (runningStat == null) {
runningStat = new RunningStat[3];
runningStat[0] = new RunningStat(3);
runningStat[1] = new RunningStat(3);
runningStat[2] = new RunningStat(3);
} else {
runningStat[0].clear();
runningStat[1].clear();
runningStat[2].clear();
}
this.sensorType = sensorType;
sensorManager = (SensorManager) context
.getSystemService(Context.SENSOR_SERVICE);
Sensor sensor = sensorManager.getDefaultSensor(sensorType);
sensorManager.registerListener(this, sensor,
SensorManager.SENSOR_DELAY_NORMAL, handle);
}
public RunningStat[] finishWithResult() {
if (sensorManager != null) {
sensorManager.unregisterListener(this);
}
return runningStat;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == sensorType) {
runningStat[0].push(event.values[0]);
runningStat[1].push(event.values[1]);
runningStat[2].push(event.values[2]);
}
}
}
}
这是 RunningStat
代码,这是一个非常方便的类,用于计算数据流的均值和方差而不存储数据本身(非常适合计算高频传感器的统计数据)占用内存非常小)
//See Knuth TAOCP vol 2, 3rd edition, page 232
public class RunningStat {
private int n;
private double oldM, newM, oldS, newS;
private int precision = -1;
// An estimate for the t-value (can be read from the t-distribution table)
private static final double T_THRESHOLD = 1.68;
public RunningStat(int precision) {
this.precision = precision;
}
public RunningStat() {
}
public void clear() {
n = 0;
}
public void push(double x) {
n++;
if (n == 1) {
oldM = newM = x;
oldS = 0.0;
} else {
newM = oldM + (x - oldM) / n;
newS = oldS + (x - oldM) * (x - newM);
// set up for next iteration
oldM = newM;
oldS = newS;
}
}
public int count() {
return n;
}
public double mean() {
double mean = (n > 0) ? newM : 0.0;
if (precision > 0) {
return round(mean, precision);
}
return mean;
}
// The upper bound of the mean confidence interval
public double meanUpper() {
double mean = (n > 0) ? newM : 0.0;
double stdError = stdDeviation() / Math.sqrt(n);
double upperMean = mean + T_THRESHOLD * stdError;
if (precision > 0) {
return round((n > 0) ? upperMean : 0.0, precision);
}
return upperMean;
}
// The lower bound of the mean confidence interval
public double meanLower() {
double mean = (n > 0) ? newM : 0.0;
double stdError = stdDeviation() / Math.sqrt(n);
double lowerMean = mean - T_THRESHOLD * stdError;
if (precision > 0) {
return round((n > 0) ? lowerMean : 0.0, precision);
}
return lowerMean;
}
public double variance() {
if (precision > 0) {
return round(((n > 1) ? newS / (n - 1) : 0.0), precision);
}
return ((n > 1) ? newS / (n - 1) : 0.0);
}
public double stdDeviation() {
if (precision > 0) {
return round(Math.sqrt(variance()), precision);
}
return Math.sqrt(variance());
}
public void setPrecision(int precision) {
this.precision = precision;
}
public static double round(double value, int precision) {
BigDecimal num = new BigDecimal(value);
num = num.round(new MathContext(precision, RoundingMode.HALF_UP));
return num.doubleValue();
}
// A small test case
public static void main(String[] args) {
int n = 100;
RunningStat runningStat = new RunningStat();
double[] data = new double[n];
double sum = 0.0;
for (int i = 0; i < n; i++) {
data[i] = i * i;
sum += data[i];
runningStat.push(data[i]);
System.out.println(runningStat.mean() + " - "
+ runningStat.variance() + " - "
+ runningStat.stdDeviation());
}
double mean = sum / n;
double sum2 = 0.0;
for (int i = 0; i < n; i++) {
sum2 = sum2 + (data[i] - mean) * (data[i] - mean);
}
double variance = sum2 / (n - 1);
System.out.println("\n\n" + mean + " - " + variance + " - "
+ Math.sqrt(variance));
}
}
答案 1 :(得分:2)
您实际上是在寻求震动检测器功能,您无法阻止主线程,因为您很可能会遇到ANR错误
你可以尝试使用Jake Wharton of Action Bar Sherlock成名的https://github.com/square/seismic/tree/master/library/src/main/java/com/squareup/seismic
这几乎可以满足您的要求,您只需稍微调整一下即可满足您的要求。您可以添加onStart和onStop监听器并从启动和停止方法中激活它们并将它们与您的活动联系起来
你并不完全清楚你想要做什么,所以它很难提出进一步的建议但是我觉得你想要的东西可以在没有太多努力的情况下实现并且异步实现,从而避免了ANR&#39; s有点想到使用震动探测器作为你想要做的基础。
isShaking方法可能是您可能希望通过查看sampleCount和acceleratingCount变量来开始进行修改,以了解它们如何帮助您。
boolean isShaking() {
return newest != null
&& oldest != null
&& newest.timestamp - oldest.timestamp >= MIN_WINDOW_SIZE
&& acceleratingCount >= (sampleCount >> 1) + (sampleCount >> 2);
}
您可以调整这些值以确定要检测移动的样本数量或持续时间。
已经有一个样本列表,您可以使用它来进行计算,只需要 将其传递回onStop监听器
/** Copies the samples into a list, with the oldest entry at index 0. */
List<Sample> asList() {
List<Sample> list = new ArrayList<Sample>();
Sample s = oldest;
while (s != null) {
list.add(s);
s = s.next;
}
return list;
}
更新以回复评论 您可以通过使用在onStop侦听器回调中设置为false的标志来确定是否没有移动,并且您可以完全控制多长时间&#34;仍然&#34;并与自上次停止后的时间戳进行比较,以确定设备是否足够长,足以满足您的要求