Some basic screen-related knowledge://This is the rotation angle of the phone screen final int rotation = This.getwindowmanager (). Getdefaultdisplay (). Getorientation (); ROTATION values are: surface.rotation_0 Surface.ROTATION_90Surface.ROTATION_180Surface.ROTATION_270//This is reading the current activity The screen orientation of final int orientation = This.getresources (). GetConfiguration (). Orientation;orientation values are: configuration.orientation_portraitconfiguration.orientation_landscape//This is the screen orientation that sets the activity, The android:screenorientation= "Landscape" configuration in Androidmanifest.xml is equivalent to This.setrequestedorientation ( Activityinfo.screen_orientation_landscape)
Build. version: Various version strings.
android. Os. Build. VERSION. sdk_int: The user-visible SDK version of the framework; its possible values is defined in Build.VERSION_CODES
.
Build.version_codes: Enumeration of the currently known SDK VERSION CODES. These is the values that can is found in SDK
. Version numbers increment monotonically with each official platform release.
android.os.Build.VERSION_CODES.FROYO:June 2010: Android 2.2
The Reverse_landscape and reverse_portrait appear after Android 2.3,so we can not use them on Android 2.2 and former Devic Es.
private void Disablerotation () {final int orientation = Getresources (). GetConfiguration (). Orientation; Final int rotation = Getwindowmanager (). Getdefaultdisplay (). Getorientation (); Copied from Android docs, since we don't have these values in Froyo 2.2 int Screen_orientation_reverse_landscape = 8; int screen_orientation_reverse_portrait = 9; if (Build.VERSION.SDK_INT <= build.version_codes. FROYO) {screen_orientation_reverse_landscape = Activityinfo.screen_orientation_landscape; screen_orientation_reverse_portrait = activityinfo.screen_orientation_portrait; } if (rotation = = Surface.rotation_0 | | rotation = = surface.rotation_90) {if (orientation = = Con Figuration. orientation_portrait) {setrequestedorientation (activityinfo.screen_orientation_portrait); } else if (orientation = = Configuration.orientation_landscape) {SETrequestedorientation (Activityinfo.screen_orientation_landscape); }} else if (rotation = = Surface.rotation_180 | | rotation = = surface.rotation_270) {if ( Orientation = = configuration.orientation_portrait) {setrequestedorientation (screen_orientation_reverse_port RAIT); } else if (orientation = = Configuration.orientation_landscape) {setrequestedorientation (screen_o Rientation_reverse_landscape); } } }
Enable the rotation:
Setrequestedorientation (activityinfo.screen_orientation_unspecified);
About the rotation and orientation,on some devices,the surface.rotation_0 corresponds to orientation_landscape,but others , May orientation_portrait. So we had to the some work to judge it.
However,for some reason rotation_90 corresponds to screen_orientation_reverse_portrait on the Xoom if you use the above me Thod. So another method appears:
private void Disablerotation () {final int orientation = Getresources (). GetConfiguration (). Orientation; Final int rotation = Getwindowmanager (). Getdefaultdisplay (). Getorientation (); Copied from Android docs, since we don't have these values in Froyo 2.2 int Screen_orientation_reverse_landscape = 8; int screen_orientation_reverse_portrait = 9; if (Build.VERSION.SDK_INT <= build.version_codes. FROYO) {screen_orientation_reverse_landscape = Activityinfo.screen_orientation_landscape; screen_orientation_reverse_portrait = activityinfo.screen_orientation_portrait; } if (orientation = = configuration.orientation_portrait) {if (rotation = = surface.rotation_0| | rotation = = surface.rotation_270)//0 for phone, and for tablet {Setrequestedorientati On (activityinfo.screen_orientation_portrait); } else {SetrequestedoriEntation (screen_orientation_reverse_portrait); }} else {if (rotation = = surface.rotation_90| | rotation = = SURFACE.ROTATION_0)//+ F or phone, 0 for tablet {setrequestedorientation (activityinfo.screen_orientation_landscape); } else {setrequestedorientation (screen_orientation_reverse_landscape); } } }
I ' m not sure the note "//0 for phone, + for tablet" and "//* for phone, 0 for tablet" is right on any device,but at L East it works well on my device.so I use the latter one.
When you want to disable/enable the autorotation of the device, you have to write the settings.
Android Lock Screen Orientation