5

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.

jeevs
  • 261
  • 6
  • 20
  • so what is the question? you want `onConfigurationChanged()` event to be called whenever the orientation has changed despite the lock that you mentioned? – 10s Jul 19 '12 at 07:19
  • Exactly, i want it called every time, whenever phone orientation is changed. – jeevs Jul 19 '12 at 07:21

2 Answers2

5

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.

Community
  • 1
  • 1
10s
  • 1,699
  • 1
  • 12
  • 14
  • Yes i have to do it using either Rotation sensor or gravity+geomagnetic sensors. But now a new problem popped up that i have only accelerometer+gravity+linear acceleration+ proximity sensor+light sensors on my phone. So i can't use library methods to calculate orientation as i don't have rotation sensor or geomagnetic sensor. so anyone can help me to calculate orientation using the above available sensors. – jeevs Jul 19 '12 at 13:14
  • thanks but i have seen this code on developer docs and it requires Magentometer which my phone (HTC Explorer) doesn't have. So i need to compute orientation using accelerometer. – jeevs Jul 20 '12 at 06:12
  • maybe you should close and confirm this topic and add another one because the problem right now has changed and has gone to a different new level. The way I see it, using just the accelerometer for the orientation change won't be easy and unfortunately I haven't done anything similar and I can't help you from now on. – 10s Jul 20 '12 at 06:51
0

onConfigurationChanged - is a notification to let your activity know the change in orientation.

You should not attempt to change or fix the orientation in that method. If you set the orientation to landscape or portrait then you will not get new orientation change notification because the orientation of the activity does not change. May be you should set the orientation setting in the event handler of the widget that you plan to show when there is a change in orientation.

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
Prakash Nadar
  • 2,694
  • 1
  • 19
  • 20