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?