0

I want max volume when I launch the app so I getStreamMaxVolume in the onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    swVolume = (Switch) findViewById(R.id.switch1);     
    AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int vol = am.getStreamVolume(AudioManager.STREAM_RING);
    int maxVol = am.getStreamMaxVolume(AudioManager.STREAM_RING);
    am.setStreamVolume(
            AudioManager.STREAM_RING,
            am.getStreamMaxVolume(AudioManager.STREAM_RING),
            0); 

then I want the switch to be turned On if the Volume is at max

    if(vol == maxVol){
        swVolume.setChecked(true);

    }

and turned Off when the volume is not at max

    if(vol < maxVol){
            swVolume.setChecked(false);
      }
    else {
            swVolume.setChecked(false);
      }     

Can someone pls explain to me why isn't the code working? is there any way to make it work?

Tommaso Barbugli
  • 11,781
  • 2
  • 42
  • 41
BEX
  • 187
  • 4
  • 21
  • Try debugging between your lines: Log.d("vol & maxvol", "vol = " + vol + ", maxvol = " + maxvol); And see if your values concord. – Montassir Ld Jan 21 '17 at 04:24
  • vol = 0 , maxVol = 15 – BEX Jan 21 '17 at 04:39
  • Well clearly you're not listening for changes in the volume since you're doing it in the "onCreate". Try putting your volume to the max and then launch, it should work since it does the check in your onCreate. You have to listen for changes in the volume with a listener and adapt the swVolume to it. – Montassir Ld Jan 21 '17 at 04:44

0 Answers0