How to detect exact orientation of device in Froyo?

后端 未结 5 1782
执念已碎
执念已碎 2021-01-15 05:18

I\'m trying to temporarily lock the orientation of the Android device, where most of the time it changes with the sensor. So what I want to do is figure out what the current

相关标签:
5条回答
  • 2021-01-15 06:00

    I'm not exactly sure what your problem is. Are you trying to determine if a device is currently oriented in landscape or portrait mode? If so you can get that information quickly with:

    Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    Point size = new Point();
    Display.getSize(size);
    
    boolean isLandscape = size.x > size.y;
    

    If you couple that with Display.getOrientation() you should have access to all the information you need.

    0 讨论(0)
  • 2021-01-15 06:04

    I found another simple solution on this website: http://sagistech.blogspot.ch/2010/12/finding-android-screen-orientation.html

    There are multiple way to get Android screen orientation (landscape/portrait), however some of them are problematic: this.getWindowManager().getDefaultDisplay().getOrientation() Is deprecated

    this.getWindowManager().getDefaultDisplay().getRotation() Works only from Android 2.2

    I Found this to be the best resource until now, since it's exists since API 1 & it's not deprecated, yet: this.getResources().getConfiguration().orientation

    Compare it with:

    • Configuration.ORIENTATION_LANDSCAPE
    • Configuration.ORIENTATION_SQUARE
    • Configuration.ORIENTATION_PORTRAIT

    For me it worked.

    0 讨论(0)
  • 2021-01-15 06:17

    I am having similar problems in Froyo

    In the activity section of the manifest file I declare:

    <activity ...
        android:configChanges="keyboardHidden|orientation">
    </activity>
    

    The main activity has the code to get the information about rotation either when the application is resumed or when a configuration change event is received:

    public void onResume() {
        super.onResume();
        showRotation();
        ...
    }
    
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        showRotation();
        ...
    }
    
    public void showRotation() {
        // int rotationType = getWindowManager().getDefaultDisplay().getOrientation(); // Deprecated
        int rotationType = getWindowManager().getDefaultDisplay().getRotation(); // API 8+ (Android 2.2.x or higher)
        Log.d(MYTAG, "rotation type: " + rotationType);
    }
    

    The problem:

    • If the device is rotated from landscape to portrait and then to reverse landscape, everything works as expected.
    • If the device is rotated from landscape to reverse landscape, the event of configuration change is not fired at all. The program still considers the device being in landscape mode.

    So, what is the solution? should the program be continuously asking to get the current orientation?

    0 讨论(0)
  • 2021-01-15 06:18

    I modified diyism's answer in a different post to work for Froyo. Since Froyo doesn't support reverse_landscape it will appear upside down but will return to the proper orientation after you unlock. In Gingerbread and later it should work without problems.

    Not a perfect solution but good enough for my needs.

    public static void disableRotation(Activity activity)
    {       
        final int orientation = activity.getResources().getConfiguration().orientation;
        final int rotation = activity.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 == Configuration.ORIENTATION_PORTRAIT)
            {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
            else if (orientation == Configuration.ORIENTATION_LANDSCAPE)
            {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }
        }
        else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270) 
        {
            if (orientation == Configuration.ORIENTATION_PORTRAIT) 
            {
                activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_PORTRAIT);
            }
            else if (orientation == Configuration.ORIENTATION_LANDSCAPE) 
            {
                activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
            }
        }
    }
    
    public static void enableRotation(Activity activity)
    {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
    }
    
    0 讨论(0)
  • 2021-01-15 06:22

    I am not sure if the constant is the issue that was no implemented on that API. Have you tried something like this?

     switch (((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay().getRotation()) {
        case Surface.ROTATION_90: 
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
            break;
        case Surface.ROTATION_180: 
            setRequestedOrientation(9); /* ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT */
            break;          
        case Surface.ROTATION_270: 
            setRequestedOrientation(8); /* ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE */
            break;
        default : 
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
        }
    
    0 讨论(0)
提交回复
热议问题