Private Orientationeventlistener Morientationlistener; Private String TAG = "mainactivity"; private int screencurorient = 2; 1 indicates a vertical screen, 2 indicates a positive horizontal screen, 3 is a reverse vertical screen, 4 indicates a reverse horizontal private final void Startorientationchangelistener () {morientationlistener = new O Rientationeventlistener (This) {@Override public void onorientationchanged (int rotation) { Determine four directions if (rotation = =-1) {LOG.D (TAG, "mobile phone flat put:" + rotation); } else if (Rotation < | | rotation >) {screenorientchange (1); } else if (rotation < && rotation >) {screenorientchange (4); } else if (rotation < && rotation >) {screenorientchange (3); } else if (Rotation < 280 && Rotation > 260) {screenorientchange (2); } else { } } }; Morientationlistener.enable (); } private void Screenorientchange (int Orient) {if (Orient! = screencurorient) {Screencuror ient = Orient; Switch (screencurorient) {case 1:LOG.D (TAG, "upright screen:"); Setrequestedorientation (activityinfo.screen_orientation_portrait); Break Case 2:LOG.D (TAG, "positive horizontal screen:"); Setrequestedorientation (Activityinfo.screen_orientation_landscape); Break Case 3:LOG.D (TAG, "anti-vertical screen:"); Setrequestedorientation (activityinfo.screen_orientation_portrait); Break Case 4:LOG.D (TAG, "Reverse horizontal screen:"); Setrequestedorientation (Activityinfo.screen_orientation_landscape); Break } } }
InonCreatemethod, you can call the Startorientationchangelistener method.
Android Phone rotation direction recognition