androidandroid-intentbroadcast

Is there a broadcast action for volume changes?


I'm programming a small widget that needs to be updated whenever the user changes the ringer volume or the vibrate settings.

Capturing android.media.VIBRATE_SETTING_CHANGED works just fine for the vibrate settings, but I haven't found any way of getting notified when the ringer volume changes and although I could try to capture when the user presses the volume up/volume down physical keys, there are many other options for changing the volume without using these keys.

Do you know if there's any broadcast action defined for this or any way to create one or to solve the problem without it?


Solution

  • There is no broadcast action, but I did find you can hook up a content observer to get notified when the settings change, volume of streams being some of those settings. Register for the android.provider.Settings.System.CONTENT_URI to be notified of all settings changes:

    mSettingsContentObserver = new SettingsContentObserver( new Handler() ); 
    this.getApplicationContext().getContentResolver().registerContentObserver( 
        android.provider.Settings.System.CONTENT_URI, true, 
        mSettingsContentObserver );
    

    The content observer might look something like this:

    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);
          Log.v(LOG_TAG, "Settings change detected");
          updateStuff();
       }
    }
    

    And be sure to unregister the content observer at some point.