Turn Over - Orientation Sensor in Android

247 Views Asked by At

I try to use the Vector Rotation sensor to find the event when the user turn over his device, as GS3 to stop music.

My code :

private SensorManager mSensorManager;
private final SensorEventListener mSensorListener = new SensorEventListener() {

    public void onSensorChanged(SensorEvent se) {
      float x = se.values[0];
      float y = se.values[1];
      float z = se.values[2];
      System.out.println("X Vector : " + x + " / Y Vector : " + y + " / Z Vector : " + z);
      if(//condition){
          //method1();
      }

    }
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR), SensorManager.SENSOR_DELAY_NORMAL);

}

I use System.out.println to see how the x, y and z variables change during my turn over, but I don't understand these values. When I let the device on the table and I start Activity, x, y and z are not always at 0. Then when I turn over it, all values change and still very close (values between -1 and 1).

My quastion is, how can I find the good axis, and what is the value which I have to put in my condition to detect the turn over ?

enter image description here

EDIT : Finally the code works fine using Y axe, but I can't use the values if landscape orientation is possible on the activity. Y axe values are corrects only if use portrait orientation only. Any idea to use with both ?

0

There are 0 best solutions below