简体   繁体   中英

Android screen rotation detection

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM