1

Can someone give me a piece of code which can use the android content observer to detect changes in the ringer volume and log it please??

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
kevdliu
  • 1,709
  • 4
  • 29
  • 46
  • This Stack Overflow [question](http://stackoverflow.com/questions/6896746/android-is-there-a-broadcast-action-for-volume-changes) has an answer that should help. – abh Oct 20 '11 at 03:41

1 Answers1

5

May be a little bit late. But I implemented the following solution. Code below will only functioning on Android < v4 devices. I am looking for a solution for the v4 devices. Maybe someone knows?:

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

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);

        try
        {
            long newRingerVolume = Settings.System.getLong(_context.getContentResolver(), Settings.System.VOLUME_RING);             
        }
        catch (SettingNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
Anthony Prune
  • 66
  • 1
  • 2
  • 4
    On Android 4+ you need to override `public void onChange (boolean selfChange, Uri uri)` see [javadoc](http://developer.android.com/reference/android/database/ContentObserver.html#onChange%28boolean,%20android.net.Uri%29) – Iwo Banas Sep 17 '13 at 15:36