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);