Orientation Lock in Android Software

I have the following code.

Java

public void lockScreenOrientation() { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); } public void unlockScreenOrientation() { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); } 

I call these functions from javascript. Management introduces these methods. However, the orientation is not locked.

I tried the following to block orientation

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); 

None of this works. Any pointers would be helpful.

+6
source share
4 answers

I have created some useful methods to help deal with orientation locks, feel free to use this class.

Usage example:

  • In action: OrientationUtils.lockOrientationPortrait(MyActivityName.this)
  • In fragment: OrientationUtils.lockOrientationLandscape(getActivity())

code:

 /** Static methods related to device orientation. */ public class OrientationUtils { private OrientationUtils() {} /** Locks the device window in landscape mode. */ public static void lockOrientationLandscape(Activity activity) { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } /** Locks the device window in portrait mode. */ public static void lockOrientationPortrait(Activity activity) { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } /** Allows user to freely use portrait or landscape mode. */ public static void unlockOrientation(Activity activity) { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); } } 

Here is my full GitHub OrientationUtils class that can be used in any Android app: https://github.com/danialgoodwin/android-simply-advanced-helper/blob/master/SimplyAdvancedHelperLibrary/src/net/simplyadvanced/utils/OrientationUtils .java

+5
source
 Activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED); 


Lock the screen (activity) in any orientation.

Requires API Level> = 18

+4
source

This is the class I wrote to handle lock and unlock screen orientation. I call toggleScreenOrientationLock(this, prefs, isChecked) using the switch button checkChangedListener and restoreScreenLock(this, prefs) from onCreate (). In both cases, this is your activity, and prefs is the SharedPrefences object used to store information about the lock state.

The getScreenOrientation() part of the code is the getScreenOrientation() function, which I stole and removed here . I will try to explain the logic of how this works.

When we set the device orientation using setRequestedOrienation() , we need to know if the device is in landscape or portrait mode, and we need to know the reverse orientation (rotated 180 degrees).

Using getResources().getConfiguration().orientation will answer the question of which orientation we are in. If we could influence the rotation of the device, we could determine if it was rotated 180 or not. Unfortunately, depending on the device, ROTATE_0 may be portrait or landscape. Phones typically display ROTATE_0 on a portrait, and tablets on a landscape.

So, the solution used here is to use the size of the screen to determine if it is in a landscape or portrait. If the screen is wider than high, then we conclude that the device is in landscape orientation, and vice versa for portraiture. We can then take the rotation into account to find out if the orientation has changed or not.

For example, if the screen is wider than tall, then we know that we are in landscape orientation. If the rotation is 0 or 180 (in the logic of the code it is! IsRotatedOrthogonally), then we know that 0 is LANDSCAPE, and 180 is REVERSE_LANDSCAPE.

It was noted elsewhere that this will not work on all devices since the 90 or 270 reverse orientation is device specific. But this is still probably the best thing you are going to do; in the worst case, one orientation will rotate 180 degrees when you close it, which is likely to happen if you try to lock the screen in any other way.

 public class ScreenLocker { final private static String ROTATION_LOCKED_KEY = "LockedOrientationVal"; final private static String ROTATION_IS_LOCKED_KEY = "IsRotationLocked"; final private static String ROTATION_SAVED_KEY = "SavedOrientationVal"; public static int getScreenOrientation(Activity activity) { final Display display = activity.getWindowManager().getDefaultDisplay(); final int rotation = display.getRotation(); Point size = new Point(); display.getSize(size); final boolean isWiderThanTall = size.x > size.y; final boolean isRotatedOrthogonally = (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270); int orientation; if (isRotatedOrthogonally) { if (isWiderThanTall) orientation = (rotation == Surface.ROTATION_90) ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE : ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; else orientation = (rotation == Surface.ROTATION_90) ? ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; // normal and reversed switched intended } else { if (isWiderThanTall) orientation = (rotation == Surface.ROTATION_0) ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE : ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; else orientation = (rotation == Surface.ROTATION_0) ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; } return orientation; } public static void toggleScreenOrientationLock(Activity activity,SharedPreferences prefs, boolean lock) { if(lock) lockScreenOrientation(activity, prefs); else unlockScreenOrientation(activity, prefs); } // call this from your activity onCreate() or onResume() public static boolean restoreScreenLock(Activity activity, SharedPreferences prefs) { final boolean isLocked = prefs.getBoolean(ROTATION_IS_LOCKED_KEY, false); final int previousLockedOrientation = prefs.getInt(ROTATION_LOCKED_KEY, -999); if(isLocked && previousLockedOrientation != -999) { prefs.edit().putInt(ROTATION_SAVED_KEY, activity.getRequestedOrientation()).apply(); activity.setRequestedOrientation(previousLockedOrientation); return true; } return false; } private static void lockScreenOrientation(Activity activity, SharedPreferences prefs) { final int currentOrientation = activity.getRequestedOrientation(); final int lockOrientation = getScreenOrientation(activity); // checking isCurrentlyLocked prevents the ROTATION_LOCKED_KEY and ROTATION_SAVED_KEY // becoming identical, which results in the screen not being able to be unlocked. final boolean isCurrentlyLocked = prefs.getBoolean(ROTATION_IS_LOCKED_KEY, false); if(!isCurrentlyLocked) { activity.setRequestedOrientation(lockOrientation); prefs.edit() .putInt(ROTATION_SAVED_KEY, currentOrientation) .putInt(ROTATION_LOCKED_KEY, lockOrientation) .putBoolean(ROTATION_IS_LOCKED_KEY, true) .apply(); } } private static void unlockScreenOrientation(Activity activity, SharedPreferences prefs) { final int savedOrientation = prefs.getInt(ROTATION_SAVED_KEY, activity.getRequestedOrientation()); activity.setRequestedOrientation(savedOrientation); prefs.edit().putBoolean(ROTATION_IS_LOCKED_KEY, false).apply(); } } 
+1
source

Here is another simple solution that works well for me.

 private void orientationManager(boolean lock) { int currentOrientation = getResources().getConfiguration().orientation; if(lock) { if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); } else { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); } } else { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); } } 

I needed to block orientationManager(true); current screen orientation when opening dialogs and unlock orientationManager(false); when the dialogue was closed.

0
source

Source: https://habr.com/ru/post/958828/