Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android natural sensor orientation help

I am trying to accomplish Reto Meier's recommended way for keeping the screen orientation from changing. The slides from his talk during Google IO (see #23) can be found in Android Protips: Where to Download the Slides and Code Snippets.

I have stepped through the code and it setting the values, but the screen orientation still changes. FYI, I register this listener in the Application.

Here is my code:

final SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    sm.registerListener(
        new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent sensorEvent) {
                if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
                    final WindowManager wm = (WindowManager) getApplicationContext()
                            .getSystemService(Context.WINDOW_SERVICE);
                    final Display display = wm.getDefaultDisplay();

                    int x = SensorManager.AXIS_X;
                    int y = SensorManager.AXIS_Y;

                    switch (display.getRotation()) {
                    case Surface.ROTATION_90:
                        x = SensorManager.AXIS_Y;
                        y = SensorManager.AXIS_MINUS_X;

                        break;
                    case Surface.ROTATION_180:
                        y = SensorManager.AXIS_MINUS_Y;

                        break;
                    case Surface.ROTATION_270:
                        x = SensorManager.AXIS_MINUS_Y;
                        y = SensorManager.AXIS_MINUS_X;

                        break;
                    case Surface.ROTATION_0:
                    default:
                        break;
                    }

                    SensorManager.remapCoordinateSystem(sensorEvent.values, x, y, new float[] {});
                }
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {

            }
        }, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION),
        SensorManager.SENSOR_DELAY_NORMAL);
like image 612
bytebender Avatar asked Dec 16 '22 12:12

bytebender


1 Answers

All 30+ lines of code that currently are not working can be replaced with some XML values in your AndroidManifest.xml file.

We have all seen

<activity android:name=".YourActivity" android:screenOrientation="portrait" ></activity>

and we all know that doesn't work well with tablets that have a default orientation of landscape. But how many of you have seen this?

<activity android:name=".YourActivity" android:screenOrientation="nosensor" ></activity>

Basically it makes it so that the device's orientation doesn't respond to the sensor. So if your default is landscape or portrait it won't change. I have tested it on my Droid X and on a Xoom and it works like I would have expected it to.

I hope this helps others.

like image 67
bytebender Avatar answered Jan 03 '23 14:01

bytebender