you have to listen to Settings.System.ACCELEROMETER_ROTATION using a content observer.
To register the content observer
getContentResolver().registerContentObserver(Settings.System.getUriFor
(Settings.System.ACCELEROMETER_ROTATION),
true,rotationObserver );
And declare it here. The onChange method will be called when the rotation is changed.
private ContentObserver rotationObserver = new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
Do your task
}
};