0

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();
                }
            }
        });

    }

}
Marinos An
  • 9,481
  • 6
  • 63
  • 96
  • Can you post the code where you are changing the SharedPref value to false? – Azhar92 Jul 04 '19 at 14:10
  • @Noudy If you are interested in kotlin, I had answered on how to simplify the use of `SharePreferences` on this link https://stackoverflow.com/a/56873719/3710341 – Sagar Chapagain Jul 04 '19 at 14:23

3 Answers3

0

get instance from preferences

sharedPreferences = getContext().getSharedPreferences(preferences, 
Activity.MODE_PRIVATE);

save to preferences

sharedPreferences.edit().putBoolean(key, value).apply();

get from preferences

sharedPreferences.getBoolean(key, defaultValue);
0

To get stored value from SharedPreferences first you have to store there something. For that you have to use SharedPreferences.Editor

Like:

SharedPreferences sp= getSharedPreferences("prefs", Context.MODE_PRIVETE);
SharedPreferences.Editor editor = sp.edit();
//1st parameter is key and 2nd is value to store
editor.putBoolean("key_son", true); 
editor.commit();

To read from SharedPreferences use:

//Declare SharedPreferences only if you didn't before
SharedPreferences sp= getSharedPreferences("prefs", Context.MODE_PRIVETE);
boolean playSound = sp.getBoolean("key_son", true);

Now you have your playSound ready to use.

ph7
  • 169
  • 8
0

Here is my main activity i success to get the boolean. Actually i have two boolean value to get. One boolean for song and one for vibrator.

The problem now is that i get the boolean of the two but when i change one to true or to false, it affect automatically the second one.

For example: playsound = true and isVibrating = true then i click on button3 and button4, everything work correctly, song is playing and vibrating is working

but when i change playsound or isVibrating to false, all of them changed also, so when i click again on button3 and button4, song is not playing but vibration also does not work, i think one change affect the all i dont know why

public class MainActivity extends AppCompatActivity {


Button button3;
Button button4;
Vibrator vibrator;

private boolean playSound;
private boolean isVibrating;

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

    vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    button3 = (Button) findViewById(R.id.button3);
    button4 = (Button) findViewById(R.id.button4);


    button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if(!SettingsActivity.playSound){
                AudioPlay.playAudio(MainActivity.this, R.raw.sonvoix);
            }
        }
    });

    button4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(!SettingsActivity.isVibrating){
             vibrator.vibrate(400);
            }

        }
    });


}

}

    <SwitchPreference
        android:defaultValue="true"
        android:key="key_son"
        android:summary="Activer ou désactiver le son de touche"
        android:title="Son" />

    <SwitchPreference
        android:defaultValue="false"
        android:key="key_vibreur"
        android:summary="Activer ou désactiver le vibreur"
        android:title="Vibreur" />

public class SettingsActivity extends PreferenceActivity {

public static boolean playSound;
public static boolean isVibrating;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getFragmentManager().beginTransaction().replace(android.R.id.content, new MainSettingsFrament()).commit();

    SharedPreferences sharedPreferencesPlaySound = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    playSound = sharedPreferencesPlaySound.getBoolean("key_musique", false);

    SharedPreferences sharedPreferencesVibrating = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    isVibrating = sharedPreferencesVibrating.getBoolean("key_vibreur", true);

}