我正在开发一款应用程序,我需要使用开关切换音频状态静音和取消静音功能。目前,开关状态是持久保存的,但我一直遇到音频状态问题,因为它在先前静音后没有取消静音。除了重新启动应用程序时,我无法取消静音。但是,静音时会保存音频状态。我该怎么办?
public class SettingsActivity extends AppCompatActivity {
private Switch mSoundSwitch;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
AudioManager aManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
mSoundSwitch = (Switch) findViewById(R.id.soundSwitch);
sharedPreferences = getPreferences(Context.MODE_PRIVATE);
boolean isSound = sharedPreferences.getBoolean("sound",false);
mSoundSwitch.setChecked(isSound);
if(mSoundSwitch.isChecked()){
unmute();
} else {
mute();
}
mSoundSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
unmute();
sharedPreferences = getPreferences(Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.putBoolean("sound", true);
editor.apply();
} else {
mute();
sharedPreferences = getPreferences(Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.putBoolean("sound", false);
editor.apply();
}
}
// retreiving value from shared prefrences (sound)
sharedPreferences = getPreferences(Context.MODE_PRIVATE);
boolean isChecked = sharedPreferences.getBoolean("sound", true);
mSoundSwitch.setChecked(isChecked);
}
private void mute() {
//mute audio
aManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
aManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_MUTE, 0);
} else {
aManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
}
}
public void unmute() {
//unmute audio
aManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
aManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_UNMUTE, 0);
} else {
aManager.setStreamMute(AudioManager.STREAM_MUSIC, false);
}
}
}
答案 0 :(得分:0)
您已保存了偏好设置,但已检索到偏好设置并仅使用SettingsActivity
。
要解决您的问题,请在应用的开头(例如onCreate
的{{1}}),检查共享偏好设置中的值并采取相应措施。
答案 1 :(得分:0)
在致电boolean isSound = sharedPreferences.getBoolean("sound",false);
之前,您应该使用setOnCheckedChangeListener
。您将知道已保存的状态
试试这个。
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
sharedPreferences = getPreferences(Context.MODE_PRIVATE);
boolean isSound = sharedPreferences.getBoolean("sound",false);
// edited here
mSoundSwitch.setChecked(isSound);
if(mSoundSwitch.isChecked()){
unmute();
} else {
mute();
}
mSoundSwitch = (Switch) findViewById(R.id.soundSwitch);
mSoundSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
unmute();
sharedPreferences = getPreferences(Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.putBoolean("sound", true);
editor.apply();
} else {
sharedPreferences = getPreferences(Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.putBoolean("sound", false);
editor.apply();
}
}
}
}