Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locking ScreenOrientation

I am making an App which allow you read Wikipedia pages.

I want to display an icon each time when is phone is rotated from the portrait to landscape or vice versa. It will let user lock the screen orientation if he wants to or else the screen is oriented according to the sensor data. This is functionality is implemented by some of the App in the google play, for example - Pocket

To do this is i have overridden the

public void onConfigurationChanged(Configuration newConfig)

Now if i am setting the locked configuration by using( orientation_dir is the orientation value stored in the shared preferences and it is correct as i have debugged through the code for it.)

if(orientation_dir == Configuration.ORIENTATION_LANDSCAPE)
        {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        }
        else if(orientation_dir == Configuration.ORIENTATION_PORTRAIT)
        {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
        }

then orientation is set correctly but then onConfigurationChanged() method is not called when the phone is rotated.

If i set the orientation this way

if(orientation_dir == Configuration.ORIENTATION_LANDSCAPE)
        {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        else if(orientation_dir == Configuration.ORIENTATION_PORTRAIT)
        {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}

then desired orientation is not set. Phone reset the orientation according to the sensor data.

I tried even by not calling the super in the method as i thought it might be setting it wrong but then it gives me the exception "Super not called".

I am trying for last 2 days and haven't got a any solution to this problem.

like image 858
jeevs Avatar asked Jul 19 '12 06:07

jeevs


1 Answers

In general onConfigurationChanged() event fire only when an configuration change has occured. In your app the orientation changed event occurs only when the screen is free to rotate. If you have locked the screen orientation then the screen orientation event is not fired. onConfigurationChanged() does not listen to the sensor that is responsible to rotate the device but fires only when the appropriate event is happening.

So what you really want is to have access to a SensorManager and attach a SensorListener. This is the way for you to listen to the actual orientation of the device.

Here is a very nice demo that demonstrates the SensorManager capabilities with the orientation of the phone:

http://www.workingfromhere.com/blog/2009/03/30/orientation-sensor-tips-in-android/

UPDATE: The orientation sensor is a composite sensor to make things easier for the developer. It does not actually exist in the phone. Is a very neat sensor combining the accelometer sensor and the magnetic field sensor. And the OrientationSensor is currently deprecetated according to the docs (http://developer.android.com/guide/topics/sensors/sensors_position.html).

See What is the alternative to android orientation sensor? for sample usage.

it may need some fixing I have not tested it much.

like image 195
10s Avatar answered Oct 17 '22 21:10

10s