Is there a way to detect device orientation events or at least to get the current device orientation while using android:screenOrientation in the manifest ? I mean device orientation not application orientation.
My problem is I really want to keep my application in portait mode (it's a web view) but I want to handle orientation change myself in my app. When android:screenOrientation is setted, orientation getted by methods is always the application orientation, not the device orientation.
Regards, Gaëtan
Yo can use the acelerometer to detect the device orientation. I know, it's more hard but I think is the only solution.
Android | Sensor Manager | getOrientation
Happy coding :)
If you want to handle device rotation yourself, then use the following method:
@Override
public void onConfigurationChanged(Configuration config)
{
super.onConfigurationChanged(config);
Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
if(display.getRotation() == Surface.ROTATION_90)
{
mFlash.startAnimation(mRotate);
}
if(display.getRotation() == Surface.ROTATION_90)
{
parameters.setPreviewSize(width, height);
}
if(display.getRotation() == Surface.ROTATION_180)
{
parameters.setPreviewSize(height, width);
}
if(display.getRotation() == Surface.ROTATION_270)
{
parameters.setPreviewSize(width, height);
mCamera.setDisplayOrientation(180);
}
}
The actions inside the if statements are just examples of what might be done. Obviously, handle whatever you need to there.
I think the nice orientation (portrait or landscape) values that you get will always give you "application orientation". For device orientation you can check out SensorManager.getOrientation.
Unfortunately the method for listening to changes in this value is marked deprecated, so you may need to poll periodically.
来源:https://stackoverflow.com/questions/5464555/detecting-device-orientation-change-using-androidscreenorientation