1

I am developing an android application whose one feature is to lock screen orientation to Landscape , I want to apply this orientation change to all the android application in phone . I am using this code

private void lockScreenOrientation() {
if (!mScreenOrientationLocked) {
    final int orientation = getResources().getConfiguration().orientation;
    final int rotation = getWindowManager().getDefaultDisplay().getOrientation();
    if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) {
        if (orientation == Configuration.ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    }
    else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270) {
        if (orientation == Configuration.ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
        }
        else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
        }
    }

    mScreenOrientationLocked = true;
}
}

private void unlockScreenOrientation() {
 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
 mScreenOrientationLocked = false;
}

But this is temporary change , doesnt take effect to all application , does anyone know a way to apply lock orientation to all applications ? Thank you

Ahmed
  • 814
  • 2
  • 19
  • 38
  • 3
    This is a guess but I'm quite sure it's impossible to lock all applications to landscape. – harism Sep 04 '12 at 15:07
  • Have look at this app , it is working same way as i wanted https://play.google.com/store/apps/details?id=com.devasque.rotationlocker&feature=search_result#?t=W251bGwsMSwxLDEsImNvbS5kZXZhc3F1ZS5yb3RhdGlvbmxvY2tlciJd – Ahmed Sep 04 '12 at 15:15
  • I agree that you probably *shouldn't* be able to..but it apparently is possible. I use an app called Set Orientation on my Nexus 7, and it can force the system to any particular orientation. – Kevin Coppock Sep 04 '12 at 15:16
  • Duplicate http://stackoverflow.com/questions/3611457/android-temporarily-disable-orientation-changes-in-an-activity/3611554#3611554 and exact code from http://stackoverflow.com/questions/6599770/screen-orientation-lock – xdumaine Sep 04 '12 at 15:19

1 Answers1

4

Applications like the one you have linked do this by modifying the global system settings values associated with rotation. Have a look at the Settings.System class for the constants available to applications. Specifically, the ACCELEROMETER_ROTATION and USER_ROTATION values will probably be of interest.

You will also need to declare the android.permission.WRITE_SETTINGS and possibly the android.permission.WRITE_SECURE_SETTINGS permissions in your manifest.

devunwired
  • 62,780
  • 12
  • 127
  • 139
  • 1
    These settings were more user friendly too compared to ``Rotation Locker`` application as this affects applications only - and most importantly - ones that do not explicitly ask for portrait mode only. – harism Sep 04 '12 at 15:55
  • @Devunwired thank you but it actually locks the current orientation nothing else , not taking affect to other apps – Ahmed Sep 04 '12 at 17:31