I am creating an Android app which has a seekbar and it controls the media volume of android device. Now, I have to update the seekbar also if there is any change in media volume of the device either by using volume button or by changing default volume seekbar of the device.
So my question is: Is there any way to listen for volume change in android so that I can update my app specific seekbar when there is a change in volume of the device?
I have found a way of doing it. But, it gets triggered whenever there is any change in Setting.
public class SettingsContentObserver extends ContentObserver {
private AudioManager audioManager;
public SettingsContentObserver(Context context, Handler handler) {
super(handler);
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
}
@Override
public boolean deliverSelfNotifications() {
return false;
}
@Override
public void onChange(boolean selfChange) {
int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
Log.d(TAG, "Volume now " + currentVolume);
}
}
Is there any better way of doing it? Please suggest. Thanks in advance