从SharePreference检索数据

时间:2019-07-04 14:06:46

标签: java android

我正在使用SharePreference进行用户界面设置。

当我想获取我的$(".success_bx p").text().split(" : ")[1]的{​​{1}}值以播放声音时,如果是或否,则为真。

但是,当我单击boolean时,即使将key_son的值更改为false,我也会一直播放歌曲,您能帮我弄清楚这是怎么回事吗?

通常,我的button3为true,为false,

当我单击按钮时,如果playsound是true,那么我会播放歌曲,否则我不会播放它,但是似乎key_son永远不会更改此值,它会保持不变

boolean playSound

3 个答案:

答案 0 :(得分:0)

从首选项中获取实例

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

保存为首选项

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

从首选项中获取

sharedPreferences.getBoolean(key, defaultValue);

答案 1 :(得分:0)

首先要从SharedPreferences获取储值,您必须在此存储一些东西。 为此,您必须使用SharedPreferences.Editor

赞:

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

要从SharedPreferences中阅读,请使用:

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

现在您可以使用playSound了。

答案 2 :(得分:0)

这是我成功获得布尔值的主要活动。其实我有两个布尔值。一个布尔值代表歌曲,一个布尔值代表振动器。

现在的问题是我得到了两个的布尔值,但是当我将一个更改为true或false时,它将自动影响第二个。

例如: playsound = true和isVibrating = true,然后我单击button3和button4,一切正常,歌曲正在播放,振动正在工作

但是当我将playound或isVibrating更改为false时,它们全部也都更改了,因此当我再次单击button3和button4时,歌曲没有播放,但振动也无法正常工作,我认为一次更改会影响我所不知道的全部为什么

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" />

公共类SettingsActivity扩展了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);

}