【问题标题】:Android Fall Detection IssueAndroid 跌倒检测问题
【发布时间】:2018-06-29 22:27:36
【问题描述】:

我正在尝试使用 android 的accelerometer 实现一个简单的跌倒检测算法。

手机当前的加速度存储在mAccel中,如果检测到突然抖动Timer t开始。 Timer 内部有两个 CountDownTimers

firstTimer 用于在用户在过去 5 秒内没有移动时注册“跌倒”,secondTimer 用于在用户未按下按钮时确认跌倒(尚未实现) .

public void onSensorChanged(SensorEvent event) {

    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        mGravity = event.values.clone();

        float x = mGravity[0];
        float y = mGravity[1];
        float z = mGravity[2];
        mAccelLast = mAccelCurrent;
        mAccelCurrent = (float) Math.sqrt(x * x + y * y + z * z);
        float delta = mAccelCurrent - mAccelLast;
        mAccel = mAccel * 0.9f + delta;
        mAccel = abs(mAccel);
        textView.setText("a:"+mAccel);
        if (abs(mAccel) > 5.0f) { // Shake detection
            t();
        }
    }
}
public void t () {

    firstTimer = new CountDownTimer(5000, 1000) {  //fall registration timer

        @Override
        public void onTick(long millisUntilFinished) {
            //if there is movement before 5 seconds pass cancel the timer
            if (abs(mAccel) > 2.0f) {
                firstTimer.cancel();
                firstTimer = null;
            }
        }

        @Override
        public void onFinish() {
            toast.show();

            secondTimer = new CountDownTimer(30*1000, 1000) { //fall confirmation timer

                @Override
                public void onTick(long millisUntilFinished) {
                    textView2.setText("" + millisUntilFinished / 1000);
                }

                @Override
                public void onFinish() {
                    Toast toast = Toast.makeText(getApplicationContext(),
                                  "Fall Registered!", Toast.LENGTH_LONG);
                        toast.show();
                    }
                }.start();
            }
        }.start();
    }

主要问题是大多数时候firstTimer被访问,由于mAccel超过2.0阈值而被取消,同时它正在减速。

我尝试使用Timer.schedule()firstTimer 的激活延迟到加速度值“休息”之后,但它无法使用它。

【问题讨论】:

    标签: java android android-studio timer scheduled-tasks


    【解决方案1】:

    我创建了一个新的 Timer 对象 mTimer,这似乎适用于所有情况。

    CountDownTimer firstTimer = new CountDownTimer(5000, 1000) {  //fall registration timer
    
            @Override
            public void onTick(long millisUntilFinished) {
                //if there is movement before 5 seconds pass cancel the timer
                if (abs(mAccel) > 2.0f) {
                    Toast toast = Toast.makeText(getApplicationContext(),
                                  "Fall not Detected", Toast.LENGTH_SHORT);
                    toast.show();
                    firstTimer.cancel();
                }
            }
    
            @Override
            public void onFinish() {
                Toast toast = Toast.makeText(getApplicationContext(),
                              "Fall Detected!", Toast.LENGTH_SHORT);
                toast.show();
                secondTimer.start();
            }
        };
    
        CountDownTimer secondTimer = new CountDownTimer(30*1000, 1000) {
        //fall confirmation timer
    
            @Override
            public void onTick(long millisUntilFinished) {
                textView2.setText("" + millisUntilFinished / 1000);
            }
    
            @Override
            public void onFinish() {
                Toast toast = Toast.makeText(getApplicationContext(),
                "Fall Registered!", Toast.LENGTH_LONG);
                toast.show();
            }
        };
    
    
    @Override
    public void onSensorChanged(SensorEvent event) {
    
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            mGravity = event.values.clone();
            float x = mGravity[0];
            float y = mGravity[1];
            float z = mGravity[2];
            mAccelLast = mAccelCurrent;
            mAccelCurrent = (float) Math.sqrt(x * x + y * y + z * z);
            float delta = mAccelCurrent - mAccelLast;
            mAccel = mAccel * 0.9f + delta;
            mAccel = abs(mAccel);
            textView.setText("a:"+mAccel);
            if (abs(mAccel) > 5.0f) { // Shake detection
                mTimer.schedule(new TimerTask() { 
                //start after 2 second delay to make acceleration values "rest"
    
                    @Override
                    public void run() {
                        firstTimer.start();
                    }
    
                }, 2000);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多