I have to solve the following: I have an Activity
which's android:screenOrientation="portrait"
. Even though, when the device is rotated to landscape while this Activity
is visible, I have to start another one, and, when the device is rotated back to portrait, I have to finish()
the activity in landscape. I tried to perform this with a BroadcastReceiver
, but this special activity doesn't receive any broadcasts because of the android:screenOrientation="portrait"
. Any help is well appreciated.
Thanks.
Philipp's solution in Get phone orientation but fix screen orientation to portrait work's perfectly for me:
You can use the SensorManager class to get the orientation of the Android device, even when the automatic orientation switching is disabled in the Manifest by android:screenOrientation="portrait"
See this code (by Philipp, see link above):
SensorManager sensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
sensorManager.registerListener(new SensorEventListener() {
int orientation=-1;;
@Override
public void onSensorChanged(SensorEvent event) {
if (event.values[1]<6.5 && event.values[1]>-6.5) {
if (orientation!=1) {
Log.d("Sensor", "Landscape");
}
orientation=1;
} else {
if (orientation!=0) {
Log.d("Sensor", "Portrait");
}
orientation=0;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With