Screen lock

I am making an application that allows you to read Wikipedia pages.

I want to display an icon every time the phone rotates from portrait to landscape or vice versa. This will allow the user to block the screen orientation if he wants, or the screen is oriented according to the sensor data. This functionality is implemented by some applications in the Google game, for example - Pocket

For this, I redefined

public void onConfigurationChanged(Configuration newConfig) 

Now, if I set the locked configuration using (orientation_dir is the orientation value stored in the general settings, and this is correct, since I debugged its code).

 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 the orientation is set correctly, but then when calling the phone, the ConfigurationChanged () method is not called.

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 the desired orientation is not set. Phone reset orientation according to sensor data.

I tried not to even call super in the method, as I thought it might be wrong, but then it gives me the exception "Super not called".

I have been trying for the past 2 days and have not received any solution to this problem.

+4
source share
2 answers

In general, the onConfigurationChanged() event fires only when a configuration is changed. In your application, a reoriented event occurs only when the screen is free to rotate. If you locked the screen orientation, then the screen orientation event will not be fired. onConfigurationChanged() does not listen to the sensor, which is responsible for the rotation of the device, but only works when the corresponding event occurs.

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

Here is a very nice demonstration demonstrating the capabilities of SensorManager with phone orientation:

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

UPDATE: the orientation sensor is a composite sensor that facilitates the work of the developer. In fact, this does not exist on the phone. It is a very neat sensor that combines an accelerometer sensor and a magnetic field sensor. And OrientationSensor is currently being canceled according to docs ( http://developer.android.com/guide/topics/sensors/sensors_position.html ).

See What is an alternative to the Android orientation sensor? to use the sample.

some correction may be required. I have not tested it.

+4
source

onConfigurationChanged is a notification letting your activity know about a change in orientation.

You should not try to change or correct the orientation in this method. If you set the orientation to landscape or portrait, you will not receive a new notification about a change in orientation, since the orientation of the activity will not change. Maybe you should set the orientation setting in the event handler of the widget that you plan to show when the orientation changes.

0
source

All Articles