Android 9 Screen Rotation Approach

230 views Asked by At

Our team has recently updated a system application (installed in priv-app) from Android 5 to Android 9. This seems to have broken the previously working (and relatively well documented approach) of programmatically flipping the device orientation.

We had been using this approach and it had worked at globablly flipping the device orientation 180 degrees:

Settings.System.putInt( context.getApplicationContext().getContentResolver(), Settings.System.USER_ROTATION, 2)

We check each time to make sure the accelerometer rotation is disabled also. The setting does in fact stick as when interrogate the settings provider and can see the value being updated, the screen is simply not reacting, even after a reboot etc..

I'm trying to understand if Android 9 introduced any further gotchas that can prevent a screen rotation from happening, would be grateful if anyone had any insight on this particular issue?

1

There are 1 answers

1
Matt On

You can instead use Activity.setRequestedOrientation() if its a particular Activity your want to change. This can be set explicitly to portrait, reverse portrait, landscape, reverse landscape, etc... It will then lock to the orientation you set. You can use the various sensor values if you want it to be flexible for the user.

If you want to know what your current orientation is (including reverse orientations) it can be complicated but I just answered my own question here about how to do it - Android sensor orientation problems with edge-to-edge insets

If you want to change the system rotation You need to ensure ACCELEROMETER_ROTATION is off and then the value you use needs to come from the surface rotation constants (which are relative to your displays natural orientation) - https://developer.android.com/reference/android/view/Surface#ROTATION_0

Another quick edit - any Activity can override this setting (including a launcher) for themselves. So it may be that whatever you are running is actually what changed to lock or allow certain orientations.