Set up Android auto-rotation to change listener

I want to listen to the “Auto-Rotate” configuration change, not the device / system orientation, but to the switching changes (on / off)
I believe that I will have to register with configManager in AndroidManifest and create a listener wherever I want, but I not sure what the correct config is. I.e.

android:configChanges='??'

But maybe another way exists, and not through android:configChanges...

+5
source share
1 answer

you need to listen to Settings.System.ACCELEROMETER_ROTATION using a content observer.

To register a content watcher

getContentResolver().registerContentObserver(Settings.System.getUriFor
(Settings.System.ACCELEROMETER_ROTATION),
true,rotationObserver );

And announce it here. The onChange method will be called when the rotation changes.

private ContentObserver rotationObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
           Do your task
        }
};
+9

All Articles