Android listener to change ringer volume and other settings?

I was wondering if there is a way to find out if the user changed the call volume or changed any settings on the phone? I’m trying to create an application with this feature: determines if the user has changed the volume of the call or changed any settings on his own device?

+3
source share
1 answer

I solved a similar problem. I had to limit the maximum volume in the application, so this is what I did (audio is an instance of AudioManager):

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP){
        if(audio != null){
            int curVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
            int maxVolume = Preferences.getDefaultMaxVolume(Main.this);
            if(curVolume > maxVolume) audio.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, 0);
        }

    } 

    return super.onKeyDown(keyCode, event);
}

You can simply use the setStreamVolume () method to change progrmatically volumes if necessary ...

+1
source

Source: https://habr.com/ru/post/1795741/


All Articles