1

这是我检测手机是否转机的代码

private SensorManager sensorManager;
    private int orientationLim = 165;

@Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {

            // If shake to stop is enabled
            boolean turnAroundToStop = Utils.getBooleanFromProperties(this, Properties.SP_CB_TURN_AROUND_TO_STOP);

            if (turnAroundToStop) {
                float value = Math.abs(event.values[1]);
                if (value > orientationLim && !stopped) {
                    // Down
                    stopped = true;
                } else {
                    // Up
                    stopped = false;
                }
            }
        }
    }

但问题是stopped即使电话没有完全转过来,变量也设置为 true 只是一点点。

我如何修改仅在电话转机时才会执行的代码。

4

1 回答 1

0
float pitch = values[2];
if (pitch <= 45 && pitch >= -45) {
    // mostly vertical
} else if (pitch < -45) {
    // mostly right side up
} else if (pitch > 45) {
    // mostly left side up
}

看看这里关于这个主题:

http://www.workingfromhere.com/blog/2009/03/30/orientation-sensor-tips-in-android/

于 2013-04-18T12:10:18.273 回答