Search code examples
androidobserver-patternvolume

Use the android content observer to detect changes in the ringer volume and log it?


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??


Solution

  • 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();
            }
    
        }
    
    }