Get phone orientation but fix screen orientation to portrait
Here is a multi-purpose class for easily managing screen orientation changes:
public class OrientationManager extends OrientationEventListener { public enum ScreenOrientation { REVERSED_LANDSCAPE, LANDSCAPE, PORTRAIT, REVERSED_PORTRAIT } public ScreenOrientation screenOrientation; private OrientationListener listener; public OrientationManager(Context context, int rate, OrientationListener listener) { super(context, rate); setListener(listener); } public OrientationManager(Context context, int rate) { super(context, rate); } public OrientationManager(Context context) { super(context); } @Override public void onOrientationChanged(int orientation) { if (orientation == -1){ return; } ScreenOrientation newOrientation; if (orientation >= 60 && orientation <= 140){ newOrientation = ScreenOrientation.REVERSED_LANDSCAPE; } else if (orientation >= 140 && orientation <= 220) { newOrientation = ScreenOrientation.REVERSED_PORTRAIT; } else if (orientation >= 220 && orientation <= 300) { newOrientation = ScreenOrientation.LANDSCAPE; } else { newOrientation = ScreenOrientation.PORTRAIT; } if(newOrientation != screenOrientation){ screenOrientation = newOrientation; if(listener != null){ listener.onOrientationChange(screenOrientation); } } } public void setListener(OrientationListener listener){ this.listener = listener; } public ScreenOrientation getScreenOrientation(){ return screenOrientation; } public interface OrientationListener { public void onOrientationChange(ScreenOrientation screenOrientation); }}
This is way more simple, reusable, and you can also get REVERSE_LANDSCAPE and REVERSE_PORTRAIT orientations.
You must implement OrientationListener in order to get notified only when an orientation change occurs.
Don't forget to call orientationManager.enable() to begin orientation tracking, and then calling orientationManager.disable() (this two methods are inherited from OrientationEventListener class)
UPDATE: use case example
MyFragment extends Fragment implements OrientationListener { ... @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); orientationManager = new OrientationManager(getActivity(), SensorManager.SENSOR_DELAY_NORMAL, this); orientationManager.enable(); } @Override public void onOrientationChange(ScreenOrientation screenOrientation) { switch(screenOrientation){ case PORTRAIT: case REVERSED_PORTRAIT: MainActivityBase.getInstance().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); break; case REVERSED_LANDSCAPE: MainActivityBase.getInstance().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); break; case LANDSCAPE: MainActivityBase.getInstance().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); break; } }}
Could you satisfy your requirement with the accelerometer? If so, perhaps something like this (untested) fragment would suit your purposes.
SensorManager sensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE); sensorManager.registerListener(new SensorEventListener() { int orientation=-1;; @Override public void onSensorChanged(SensorEvent event) { if (event.values[1]<6.5 && event.values[1]>-6.5) { if (orientation!=1) { Log.d("Sensor", "Landscape"); } orientation=1; } else { if (orientation!=0) { Log.d("Sensor", "Portrait"); } orientation=0; } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } }, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
I needed a solution that would give me the orientation only on-demand. This one worked for me:
public class SensorOrientationChecker {public final String TAG = getClass().getSimpleName();int mOrientation = 0;private SensorEventListener mSensorEventListener;private SensorManager mSensorManager;private static SensorOrientationChecker mInstance;public static SensorOrientationChecker getInstance() { if (mInstance == null) mInstance = new SensorOrientationChecker(); return mInstance;}private SensorOrientationChecker() { mSensorEventListener = new Listener(); Context applicationContext = GlobalData.getInstance().getContext(); mSensorManager = (SensorManager) applicationContext.getSystemService(Context.SENSOR_SERVICE);}/** * Call on activity onResume() */public void onResume() { mSensorManager.registerListener(mSensorEventListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);}/** * Call on activity onPause() */public void onPause() { mSensorManager.unregisterListener(mSensorEventListener);}private class Listener implements SensorEventListener { @Override public void onSensorChanged(SensorEvent event) { float x = event.values[0]; float y = event.values[1]; if (x<5 && x>-5 && y > 5) mOrientation = 0; else if (x<-5 && y<5 && y>-5) mOrientation = 90; else if (x<5 && x>-5 && y<-5) mOrientation = 180; else if (x>5 && y<5 && y>-5) mOrientation = 270; //Log.e(TAG,"mOrientation="+mOrientation+" ["+event.values[0]+","+event.values[1]+","+event.values[2]+"]"); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { }}public int getOrientation(){ return mOrientation; }}