title: How to detect orientation change from the statusbar
body: <p>Under the Statusbar, there are five buttons - Wifi, Bluetooth, GPS, Silent Mode and Auto Rotation. I haven't figured out a way yet to detect when a user click the Auto Rotation button.</p>
  
  <p>What I have done is have a Service that listen for orientation changes, like:</p>
  
  <pre><code>public class MyService extends Service {
  
      @Override
      public void onCreate() {
         super.onCreate();
         IntentFilter intentFilter = new IntentFilter();
         intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
         this.registerReceiver(myReceiver, intentFilter);     
      }
  
      public BroadcastReceiver myReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
            if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
               // Show a message about orientation changes.
               // This method is only called when the phone is physically rotated.
            }
         }
      }  
  }
  </code></pre>
  
  <p>The above BroadcastReceiver will only work if the phone is physically rotated, but if the Auto Rotation button is clicked without any physical rotation of the phone, then the BroadcastReceiver method is never called.</p>
  
  <p>I have also tried OrientationEventListener - but same behaviour as the BroadcastReceiver method.</p>
  
  <p>To set orientation, I used the following method:</p>
  
  <pre><code>Settings.System.getInt(context.getContentResolver(), "accelerometer_rotation")
  </code></pre>
  
  <p>So I know there's a method to interrogate System settings. Is there an Intent that traps system setting's [orientation] changes? I've gone through Android docs but can't find one - perhaps, I'm looking in the wrong place, or there's no Intent for it.</p>
  
  <p>Does anyone know a way to trap changes to the Auto Rotation button in the Statusbar?</p>
  
  <p><strong>UPDATE 1</strong>: The devices I'm working with are: Samsung Galaxy S3, Samsung Galaxy S2, Samsung Galaxy S and Samsung Galaxy Ace and many other. All of these devices are running stock ROM and have the Auto Rotation button under the Statusbar.</p>
  
  <p><strong>UPDATE 2</strong>: There have been suggestions advising to use the ContentObserver to listen for changes to system setting, for example:</p>
  
  <pre><code>public class SettingsContentObserver extends ContentObserver {
      public SettingsContentObserver(Handler handler) {
         super(handler);
      }
  
      @Override
      public boolean deliverSelfNotifications() {
         return super.deliverSelfNotifications(); 
      }
  
      @Override
      public void onChange(boolean selfChange) {
          super.onChange(selfChange);
          Logging.writeDebug("Settings change detected: " + selfChange);
      }
  
      public void registerContentObserver(Context context) {
          context.getContentResolver().registerContentObserver(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, this);
      }
  
      public void unregisterContentObserver(Context context) {
         context.getContentResolver().unregisterContentObserver(this);
      }
  }
  </code></pre>
  
  <p>However, I found that the <code>selfChange</code> variable in <code>onChange</code> method always return <code>false</code>.</p>