I am using SharePreference for User Interface Setting.
When i want to get the boolean
value of my key_son
to play a sound or not if it true or false.
However, when i click button3
, i always play the song even i change the value of key_son
to false, can you help me figure out what it is wrong?
normally, my boolean playSound
should be true of false,
when i click the button, if playsound is true, then i play the song, othewise i do not play it, but it seems that the boolean playSound
never change this value, it stay true
public class MainActivity extends AppCompatActivity {
Button button;
Button button2;
Button button3;
TextView textView;
MediaPlayer mediaPlayer;
private boolean playSound;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
textView = (TextView) findViewById(R.id.textView);
mediaPlayer = new MediaPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.sonvoix);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
playSound = sharedPreferences.getBoolean("key_son", true);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(playSound){
mediaPlayer.start();
}else{
mediaPlayer.stop();
}
}
});
}
}