对不起,如果我的问题看起来太具体了,不过就这样了。
我尝试做的事情:编写一个应用程序来检测摔倒(老人)并发出通知。
问题:应用程序卡在某处,最终根据LogCat跳过帧。它也会卡在一个状态,有时可能与跳帧有关,也可能没有。
目前,我正在处理我检测到坠落的部分,我想在UI中更改ImageView以指示发生了跌落。我已经弄清楚了我是如何检测掉跌倒的,并将其作为状态机实现。
起初,我只使用实现SensorEventListener的服务。这很好,但出乎意料的是,我开始注意跳帧通知。然后我读到Service在主线程上运行,这似乎是当时问题的根源。因此,经过一些搜索,我决定尝试运行一个AsyncTask,从服务中实现SensorEventListener。这根本没有帮助,阻塞了LogCat,进一步造成混乱。该应用程序当然已经构建并运行,但是仍然存在框架问题并且我仍然没有获得理想的结果。我喜欢测试我的检测跌倒的方法是否真的有效,但是我无法确定框架等是怎么回事。
这是我的全部服务代码:
public class DetectionService extends Service{
private static final String TAG = "DetectionService";
private static final float g_2 = 96.2361f; // gravity squared
private static final float FREEFALL_THRESHOLD_MULTIPLIER = 0.21f; // when acceleration approx. equal 4.5 m/s/s
private static final float IMPACT_THRESHOLD_MULTIPLIER = 2.1f; // when acceleration approx. equal 20 m/s/s
private SensorManager mSensorManager = null;
private Sensor accSensor;
private Sensor linAccSensor;
private Sensor gyroSensor;
private Thread reporter;
private Handler handler;
private CountDownTimer debounceTimer; // used to debounce signal
private CountDownTimer stillnessTimer; // used for detecting stillness longer than 5 seconds
private boolean fallen = false;
private boolean timeUp = false;
private boolean isStill = false;
private int state = 1; // 0 = fallen, 1 = detecting free fall, 2 = detecting impact, 3 = waiting 1 second, 4 = detecting stillness
public DetectionService() {
state = 1; // initial state, subject is fine
fallen = false;
stillnessTimer = new CountDownTimer(5000, 1) { // count down 5 seconds
// call every millisecond
public void onTick(long millisUntilFinished) {
if(millisUntilFinished % 1000 == 0) // every second
onSecondTick(millisUntilFinished);
if(!isStill) {
state = 1; // subject is probably fine, start waiting for free fall again
}
else {
state = 0; // subject has fallen, distress signal will be asserted
}
}
// call every second
public void onSecondTick(long millisUntilFinished) {
Log.d(TAG, "state: " + state + " " + "Time elapsed since impact: " + (5 - millisUntilFinished/1000) + " seconds");
}
public void onFinish() {
Log.d(TAG, "state: " + state + " " + "5 seconds has passed and subject has not moved.");
timeUp = true;
}
};
debounceTimer = new CountDownTimer(1000, 1000) {
// call every millisecond
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Log.d(TAG, "state: " + state + " 1 second has passed to let subject settle.");
state = 4;
}
};
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.d(TAG, "onStart");
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
}
};
reporter = new Thread(new Runnable(){
public void run() {
while(true)
{
try {
Thread.sleep(1000); // execute every second
handler.sendEmptyMessage(0);
// TO DO
if(fallen) {
// send distress signal
getApplicationContext().sendBroadcast(new Intent("distress"));
Log.d(TAG, "distress signal");
}
else {
// send fine signal
getApplicationContext().sendBroadcast(new Intent("fine"));
Log.d(TAG, "fine signal");
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
reporter.start();
SensorTask st = new SensorTask();
st.execute(null, null, null);
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public class SensorTask extends AsyncTask<Void, Void, Void> implements SensorEventListener{
private void initSensors() {
mSensorManager = (SensorManager) DetectionService.this.getSystemService(SENSOR_SERVICE);
accSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
linAccSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
gyroSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
mSensorManager.registerListener(this, accSensor,
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(this, gyroSensor,
SensorManager.SENSOR_DELAY_FASTEST);
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
switch(state) {
case 0:
Log.d(TAG, "state: " + state + " fallen: " + fallen);
fallen = true;
break;
case 1: // detecting free fall
switch(event.sensor.getType()) {
case(Sensor.TYPE_ACCELEROMETER):
if(detectFreeFall(event)) {
// free fall detected
state = 2; // start detecting impact
}
break;
default:
break;
}
break;
case 2: // detecting impact
switch(event.sensor.getType()) {
case(Sensor.TYPE_ACCELEROMETER):
if(detectImpact(event)) {
// impact detected
state = 3;
}
break;
default:
break;
}
break;
case 3: // waiting 1 second for phone to settle
debounceTimer.start(); // filter out the 1st second after impact to let the phone's position settle
break;
case 4: // detecting stillness
debounceTimer.cancel();
switch(event.sensor.getType()) {
case(Sensor.TYPE_GYROSCOPE):
if(!detectMovementGyro(event)) {
stillnessTimer.cancel(); // significant change in orientation detected, stop the timer
}
else {
stillnessTimer.start();
}
break;
default:
break;
}
break;
default:
break;
}
}
private boolean detectMovementGyro(SensorEvent event) {
float gyroX = event.values[0];
float gyroY = event.values[1];
float gyroZ = event.values[2];
float sqrMag = calcSqrMagnitude(gyroX, gyroY, gyroZ);
Log.d(TAG, "state: " + state + " gyro sqr mag: " + sqrMag);
Log.d(TAG, "state: " + state + " gyro: (" + gyroX + ", " + gyroY + ", " + gyroZ + ")");
if( (sqrMag > 0.2f)) { // placeholder test value for now
return true;
}
return false;
}
private boolean detectImpact(SensorEvent event) {
float accX = event.values[0];
float accY = event.values[1];
float accZ = event.values[2];
float sqrMag = calcSqrMagnitude(accX, accY, accZ);
Log.d(TAG, "state: " + state + " acc sqr mag: " + sqrMag);
if(sqrMag > (IMPACT_THRESHOLD_MULTIPLIER * g_2)) {
return true;
}
return false;
}
private boolean detectFreeFall(SensorEvent event) {
float accX = event.values[0];
float accY = event.values[1];
float accZ = event.values[2];
float sqrMag = calcSqrMagnitude(accX, accY, accZ);
Log.d(TAG, "state: " + state + " acc sqr mag: " + sqrMag);
if(sqrMag < (FREEFALL_THRESHOLD_MULTIPLIER * g_2)) {
return true;
}
return false;
}
public float calcSqrMagnitude(float x, float y, float z) {
return (float) ( Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2) );
}
@Override
protected Void doInBackground(Void... params) {
initSensors();
return null;
}
}
}
如果它有助于说明那里应该发生什么,这是我的ASM图表http://imgur.com/a6UcVPa 代码中的实现有点不同;我试着让它等待1秒钟,粗略地过滤掉装置撞击地面的任何动作,而不是作为坠落者的实际移动。
所以我想我的问题/请求是:我的方法有什么问题吗?有人能看出我是否有任何明显的noobie错误吗?
其他信息:在将SensorEventListener组件迁移到AsyncTask之前,服务卡在state = 4中。