Android运动传感器帮助

时间:2011-06-05 12:27:44

标签: android sensor

我从网站http://www.clingmarks.com/?p=25获得了此代码,我希望得到一些帮助!

此代码记录手机抖动。它只允许每100毫秒更新一次,并且震动的值必须超过800,SHAKE_THRESHOLD。

我遇到的问题是,如果手机检测到摇晃(然后是变量i ++),我希望在一段时间过去之前无法访问该代码块。

有人可以帮助我吗?

public class ShakeActivity extends Activity implements SensorListener {
    // For shake motion detection.
    private SensorManager sensorMgr;
    private long lastUpdate = -1;
    private float x, y, z;
    int i = 0;
    private float last_x, last_y, last_z;
    private static final int SHAKE_THRESHOLD = 800;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...... // other initializations
            // start motion detection
            sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
        boolean accelSupported = sensorMgr.registerListener(this,
                SensorManager.SENSOR_ACCELEROMETER,
                SensorManager.SENSOR_DELAY_GAME);

        if (!accelSupported) {
            // on accelerometer on this device
            sensorMgr.unregisterListener(this,
                    SensorManager.SENSOR_ACCELEROMETER);
        }
    }

    protected void onPause() {
        if (sensorMgr != null) {
            sensorMgr.unregisterListener(this,
                    SensorManager.SENSOR_ACCELEROMETER);
            sensorMgr = null;
        }
        super.onPause();
    }

    public void onAccuracyChanged(int arg0, int arg1) {
        // TODO Auto-generated method stub
    }

    public void onSensorChanged(int sensor, float[] values) {
        if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
            long curTime = System.currentTimeMillis();
            // only allow one update every 100ms.
            if ((curTime - lastUpdate) > 100) {
                long diffTime = (curTime - lastUpdate);
                lastUpdate = curTime;

                x = values[SensorManager.DATA_X];
                y = values[SensorManager.DATA_Y];
                z = values[SensorManager.DATA_Z];

                float speed = Math.abs(x+y+z - last_x - last_y - last_z)
                    / diffTime * 10000;
                if (speed > SHAKE_THRESHOLD) {
                    // yes, this is a shake action! Do something about it!
                    i++;
                }
                last_x = x;
                last_y = y;
                last_z = z;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

使用System.currentTimeMillis()为注册摇动时设置变量。然后当你再次摇晃时,将当前时间与上次摇晃时间进行比较。如果超过所需的延迟,请注册新的摇动并更新最后的摇动时间。如果没有,请不要做任何事情。

答案 1 :(得分:1)

您想要的行为被称为“缓和”。您想在Activity对象上设置某个变量:

    private long last_shake_time = 0;

然后检查并更新注册摇动的代码:

            if (speed > SHAKE_THRESHOLD) {
                // yes, this is a shake action!
                long shake_time = System.currentTimeMillis();
                if (shake_time - last_shake_time >= SHAKE_HOLDOFF_MS) {
                    i++;
                    last_shake_time = shake_time;
                }
            }

SHAKE_HOLDOFF_MS定义为SHAKE_THRESHOLD常量:

    private static final long SHAKE_HOLDOFF_MS = 5000;