Device Orientation Detection

I need to determine the orientation change of an Android device without manually playing with the sensor data, while maintaining the orientation of the activity towards some orientation.

onConfigurationChange will not work as my activity will not rotate.

Playing with the sensor data to detect a change in orientation, I believe that as an invention of the wheel, since the android already has a built-in implementation of the algorithm for detecting a change in the orientation of the device. And on the other hand, detecting a change in orientation is not a simple check like this.

    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER || event.values.length < 3) {
            return;
        }
        //getResources().getConfiguration().orientation;
        if (event.values[1] < 6.5 && event.values[1] > -6.5) {
            if (orientation!=1) {
                Log.d("Sensor", "Protrait");
            }
            orientation=1;
        } else {
            if (orientation!=0) {
                Log.d("Sensor", "Landscape");
            }
            orientation=0;
        }
   }

It really requires real data processing, such as filtering from noise and sudden short jolts / turns.

, , ? ( , , onConfigurationChange )

+5
3

Orientation-Event.

SO , .

,

+8

. .

    myOrientationEventListener = new OrientationEventListener(this,SensorManager.SENSOR_DELAY_NORMAL) {
        public void onOrientationChanged(int arg0) {
            if (arg0>=315 || arg0<45){
                currentOrientation = Surface.ROTATION_90;
            }else if (arg0>=45 && arg0<135){
                currentOrientation = Surface.ROTATION_180;
            }else if (arg0>=135 && arg0<225){
                currentOrientation = Surface.ROTATION_270;
            }else if (arg0>=225 && arg0<315){
                currentOrientation = Surface.ROTATION_0;
            }
        }
    };
+5

By setting the property screenOrientationin the manifest, you can no longer get the orientation value from onConfigurationChanged, since it will stop shooting, it is possible to implement it SensorEventListenerto get it Pitch, rolland yawto calculate the orientation, but this is a little more complicated and overwhelms this task. The best solution I found is to use OrientationEventListener, the following code will update the rotation value when the orientation changes, the value will be from 0 to 4 according to the rotation angle:

private OrientationEventListener listener;
private int rotation;

@Override
protected void onCreate (Bundle savedInstanceState)
{
    super.onCreate (savedInstanceState);
    setContentView (R.layout.main);

    listener = new OrientationEventListener (this, SensorManager.SENSOR_DELAY_NORMAL)
    {
        @Override
        public void onOrientationChanged (int orientation)
        {
            rotation = ((orientation + 45) / 90) % 4;
        }
    };

    if (myOrientationEventListener.canDetectOrientation()) listener.enable();
    else listener = null; // Orientation detection not supported
}

@Override
protected void onDestroy()
{
    super.onDestroy();
    if (listener != null) listener.disable();
}
+5
source

All Articles