3

I have a ContentObserver registered to android.provider.Settings.System that should observe changes of any audio volume. It is notified as expected when clicking the hardware volume buttons but is not notified when I change the audio volume via AudioManager.setStreamVolume or AudioManager.adjustStreamVolume.

Here's how my ContentObserver looks like:

// this is a Service
this.getApplicationContext().getContentResolver().registerContentObserver(
  android.provider.Settings.System.CONTENT_URI, 
  true, new ContentObserver(new Handler()) {
    public void onChange(boolean selfChange) {
      Log.d("ContentObserver", "got notified");
    }
});

And here my call to AudioManager.adjustStreamVolume:

// this.context is the activities context
this.context.getSystemService(Context.AUDIO_SERVICE).adjustStreamVolume(
  AudioManager.STREAM_RING, AudioManager.ADJUST_LOWER,
  AudioManager.FLAG_SHOW_UI);

I have read this and that post and the AudioManager and Settings.System documentation and can not find a reason why the Observer is notified when changing the volume with the volume buttons but not when changing it with the AudioManager.

Thanks for the help!

Community
  • 1
  • 1
Dominik Schreiber
  • 2,629
  • 1
  • 23
  • 33

1 Answers1

4

The AudioManager does broadcast an intent

android.media.VOLUME_CHANGED_ACTION`

But this is not part of the official documentation. So this might change in future releases. But you could use this atleast for gingerbread devices.

you can find more about the extras in the intent from here

nandeesh
  • 24,740
  • 6
  • 69
  • 79
  • I still don't really know why the Service is not notified when adjustStreamVolume is called, but this answer solves my problem that it *should be notified*. I've found out that the volume buttons don't call set/adjustStreamVolume but shouldn't everything result in a change of Settings.System? – Dominik Schreiber Feb 10 '12 at 19:41
  • Looking at the link that points to the extras in the intent, this event is broadcast only when the SCO volume changes ... – ekawas Jul 01 '13 at 13:37
  • What do we import for content observer? Thanks! – Ruchir Baronia Feb 07 '16 at 03:00