[英]Retrieving Data From SharePreference
I am using SharePreference for User Interface Setting. 我正在使用SharePreference进行用户界面设置。
When i want to get the boolean
value of my key_son
to play a sound or not if it true or false. 当我想获取我的key_son
的boolean
值以播放声音时(如果是或否)。
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? 但是,当我单击button3
,即使我将key_son
的值key_son
为false,我也总是播放歌曲,您能帮我弄清楚这是怎么回事吗?
normally, my boolean playSound
should be true of false, 通常,我的boolean playSound
应该为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 当我单击按钮时,如果playsound为true,那么我会播放歌曲,否则我不会播放它,但是boolean playSound
似乎永远不会更改此值,它将保持为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();
}
}
});
}
}
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);
To get stored value from SharedPreferences
first you have to store there something. 要首先从SharedPreferences
获取存储值,您必须在此处存储一些内容。 For that you have to use SharedPreferences.Editor
为此,您必须使用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: 要从SharedPreferences
读取,请使用:
//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. 现在,您可以使用playSound
了。
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. 现在的问题是我得到了两者的布尔值,但是当我将一个更改为true或false时,它将自动影响第二个。
For example: playsound = true and isVibrating = true then i click on button3 and button4, everything work correctly, song is playing and vibrating is working 例如:playsound = true和isVibrating = true,然后我单击button3和button4,一切正常,歌曲正在播放并且振动在工作
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 但是当我将playsound或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" />
public class SettingsActivity extends PreferenceActivity { 公共类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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.