简体   繁体   English

如何使用加速度计检测X运动

[英]How to detect X motion using Accelerometer

How to detect that phone was moved fe 10 meters in 2s using Accelerometer? 如何使用加速度计检测手机在2秒内移动了10米? I need to track when motion starts to switch on GPS. 我需要跟踪运动何时开始打开GPS。

    @Override
        public void onSensorChanged(SensorEvent event) {
            float x = event.values[0];
            float y = event.values[1];
            float z = event.values[2];

            float accel = FloatMath.sqrt(x * x + y * y + z * z);
            g = g * 0.9f + accel * 0.1f;

            if ((System.currentTimeMillis() - mSensorTimestamp) < 2000) return;

             //What I have to do here?

            mSensorManager.unregisterListener(this);
        }

I think you are mistaken, the accelerometer does not work this way, like a position tracker. 我认为您错了,加速度计无法像位置跟踪器那样工作。 Check The motion docs on d.android.com to see what I mean. 在d.android.com上查看Motion文档,以了解我的意思。

From the docs: 从文档:

Accelerometers use the standard sensor coordinate system. 加速度计使用标准传感器坐标系。 In practice, this means that the following conditions apply when a device is laying flat on a table in its natural orientation: 实际上,这意味着当设备以自然方向平放在桌子上时,以下条件适用:

If you push the device on the left side (so it moves to the right), the x acceleration value is positive. 如果将设备推向左侧(因此向右移动),则x加速度值为正。 If you push the device on the bottom (so it moves away from you), the y acceleration value is positive. 如果将设备推到底部(因此它向远离您的方向移动),则y加速度值为正。 If you push the device toward the sky with an acceleration of A m/s2, the z acceleration value is equal to A + 9.81, which corresponds to the acceleration of the device (+A m/s2) minus the force of gravity (-9.81 m/s2). 如果您以A m / s2的加速度将设备推向天空,则z加速度值等于A + 9.81,它对应于设备的加速度(+ A m / s2)减去重力(- 9.81 m / s2)。 The stationary device will have an acceleration value of +9.81, which corresponds to the acceleration of the device (0 m/s2 minus the force of gravity, which is -9.81 m/s2). 固定设备的加速度值为+9.81,该值对应于设备的加速度(0 m / s2减去重力-9.81 m / s2)。

So, I'd recommend you to use the Location Service directly ! 因此,建议您直接使用位置服务

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM