3

I have an app that forces screen rotation to landscape (external application controling device over ADB)

I have another app running on my device that have to detect current orientation (portrait/landscape, reverse or not doesn't matter) and do something when device screen orientation change. I have written an OrientationEventListener which works well :

m_orientationEventListener = new OrientationEventListener(this) { @Override public void onOrientationChanged(int orientation) { doSomething() } }; m_orientationEventListener.enable(); 

My problem : When the screen turns off and then on, the stock ROM rotate the screen to portrait until the screen has been unlocked. ** I neither receive an event when the screen rotates to portraits nor when it rotates back to landscape after unlock **

Any way to listen to this ?

Note: That I have no Activity on screen : i'm running in background and want to detect it from background.

Thanks for your help

1
  • 1
    use SensorEventListener. Commented May 26, 2016 at 10:49

1 Answer 1

4

Thanks +satnam singh

Here is the sample code I used :

m_sensorEventListener = new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { Log.i(TAG,"Orientation : PORTRAIT"); } else { Log.i(TAG,"Orientation : LANDSCAPE"); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) {} }; SensorManager sm = (SensorManager)getSystemService(SENSOR_SERVICE); sm.registerListener(m_sensorEventListener, sm.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR), SensorManager.SENSOR_DELAY_NORMAL); 

I have added a screen status (ON/OFF) BroadcastReceiver to shutdown my listener when screen goes off and avoid unwanted power consumption

Sign up to request clarification or add additional context in comments.

1 Comment

but getResources().getConfiguration().orientation will never give you different orientation in background, it can only be used with foreground activities...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.