私はAndroidでゲームを開発しています。スイッチで停止し、それがパラメータの他のアクティビティにあるバックグラウンドミュージックを持っています。音楽はメインメニューにあります。パラメータにメインメニューから行くことによって、私はスイッチの状態を保存する方法を知りたいスイッチの状態を保存するには
(チェックまたはチェックされていない)
この私のスイッチのためのコード:
buttonmusique = (Switch) findViewById(R.id.switchMusique);
buttonmusique.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.e("bouton", String.valueOf(buttonmusique.isChecked()));
// If the music is playing
if (isChecked) {
Intent music = new Intent();
music.setClass(Parametres.this, BackgroundMusic.class);
startService(music);
musicBoolean = true;
buttonmusique.setText("Musique On"); //To change the text near to switch
Log.d("You are :", "Checked");
} else {
Intent music = new Intent();
music.setClass(Parametres.this, BackgroundMusic.class);
stopService(music);
musicBoolean = false;
// Resume the music player
buttonmusique.setText("Musique OFF"); //To change the text near to switch
Log.d("You are :", " Not Checked");
}
}
});
'sharedPreferences'を使って、スイッチの現在の状態を保存することができますhttps:/ /developer.android.com/reference/android/content/SharedPreferences.html – Roljhon