How to detect exact orientation of device in Froyo?

后端 未结 5 1835
执念已碎
执念已碎 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:17

    I am having similar problems in Froyo

    In the activity section of the manifest file I declare:

    
    
    

    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?

提交回复
热议问题