0

I seem to have an error here. i am trying to change the accelerometer auto-rotate value in the settings.

Now, i manage to lock and un-lock rotating the device. However, whenever i lock the device it goes into portrait mode, no matter in what orientation i was while locking it.

Here is my code:

    public void setAutoOrientationEnabled(boolean enabled)
{
  Settings.System.putInt(content, Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
}

Thanks for any help!

arielschon12
  • 5,846
  • 11
  • 33
  • 49
  • That's what the settings does when you set it in Android's system settings. – zapl May 23 '12 at 14:10
  • So how can i set it to lock the current orientation on all applications? – arielschon12 May 23 '12 at 14:18
  • You can't if the app is not written by you. If it is you can set the orientation manually either in the manifest xml and / or in code like [here](http://stackoverflow.com/questions/1512045/how-to-disable-orientation-change-in-android) or [here](http://stackoverflow.com/questions/3611457/android-temporarily-disable-orientation-changes-in-an-activity) – zapl May 23 '12 at 14:34
  • You sure can. Have a look at this application, it does exactly that: https://play.google.com/store/apps/details?id=com.coinsoft.android.orientcontrol&feature=search_result#?t=W251bGwsMSwxLDEsImNvbS5jb2luc29mdC5hbmRyb2lkLm9yaWVudGNvbnRyb2wiXQ.. – arielschon12 May 23 '12 at 14:41
  • Oops, you are right. Settings [Settings.System.USER_ROTATION](http://developer.android.com/reference/android/provider/Settings.System.html#USER_ROTATION) should do the trick then – zapl May 23 '12 at 14:49
  • Cant seem to find it... im using API level 8, FROYO. – arielschon12 May 23 '12 at 14:58
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11647/discussion-between-arielschon12-and-zapl) – arielschon12 May 23 '12 at 15:03

2 Answers2

1

This code sample lock/unlock screen rotation with preserving current orientation when locked:

if (Settings.System.getInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION) == 1) {
            Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
            Settings.System.putInt( context.getContentResolver(), Settings.System.USER_ROTATION, defaultDisplay.getRotation());
            Settings.System.putInt( context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);       
} else {
            Settings.System.putInt( context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);                
}
Darick
  • 11
  • 2
0
   int orientation = this.getRequestedOrientation();
        int rotation = ((WindowManager) this.getSystemService(
                Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
        switch (rotation) {
            case Surface.ROTATION_0:
                Settings.System.putInt(getContentResolver(), Settings.System.USER_ROTATION, 1);
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                break;
            case Surface.R`enter code here`OTATION_90:
                Settings.System.putInt(getContentResolver(), Settings.System.USER_ROTATION, 0);
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                break;
        }

        this.setRequestedOrientation(orientation);
H Zan
  • 119
  • 1
  • 12