如果我的问题看起来太具体,我很抱歉,但这里有。
我正在尝试做的事情:编写一个检测(老人)跌倒并发出通知的应用程序。
问题:应用程序卡在某个地方,根据 LogCat,它最终会跳帧。它有时也会陷入一种状态,这可能与跳帧有关,也可能与跳帧无关。
目前,我正在处理检测跌倒的部分,并且我想更改 UI 中的 ImageView 以指示发生跌倒。我已经想出了如何检测跌倒,并将其作为状态机来实现。
起初,我只使用实现 SensorEventListener 的服务。这在一段时间内很好,但出乎意料的是,我开始注意到跳帧通知。然后我读到 Service 在主线程上运行,这在当时似乎是问题的根源。因此,经过一番搜索,我决定尝试从服务中运行实现 SensorEventListener 的 AsyncTask。这根本没有帮助,并且堵塞了 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 秒钟,以粗略地过滤掉设备因撞击地面而产生的任何运动,而不是作为跌倒者的实际运动。
所以我想我的问题/要求是:我的方法有什么问题吗?有人可以看看我是否犯了任何明显的新手错误吗?
附加信息:在我将 SensorEventListener 组件迁移到 AsyncTask 之前,该服务陷入状态 = 4。