我对Android很新,目前正在开发一个测验应用程序,当用户使用该应用程序时我会发出声音。我的设置活动中有一个开关按钮,可以打开和关闭应用程序的声音。 我已经实现了一些逻辑但它还没有工作。我已经筋疲力尽了,我需要一些帮助。
style.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:soundEffectsEnabled">true</item>
</style>
<style name="AppThemeMute" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:soundEffectsEnabled">false</item>
</style>
MyApplication.java
public class MyApplication extends Application {
private static int sTheme;
public final static int THEME_SOUND_ON = 0;
public final static int THEME_SOUND_OFF = 1;
@Override
public void onCreate() {
super.onCreate();
}
public static void changeToTheme(Context context, int theme) {
sTheme = theme;
switch (sTheme) {
default:
case THEME_SOUND_ON:
context.setTheme(R.style.AppTheme);
break;
case THEME_SOUND_OFF:
context.setTheme(R.style.AppThemeMute);
break;
}
}
}
SettingsActivity.java
public class SettingsActivity extends AppCompatActivity {
private Switch mSoundSwitch;
private Switch mAdsSwitch;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
// handling switch button
mSoundSwitch = (Switch) findViewById(R.id.soundSwitch);
mSoundSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
sharedPreferences = getPreferences(Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.putBoolean("sound", true);
editor.apply();
// Change Whole App Theme
MyApplication.changeToTheme(getApplicationContext(), MyApplication.THEME_SOUND_ON);
} else {
sharedPreferences = getPreferences(Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.putBoolean("sound", false);
editor.apply();
MyApplication.changeToTheme(getApplicationContext(), MyApplication.THEME_SOUND_OFF);
}
}
});
sharedPreferences = getPreferences(Context.MODE_PRIVATE);
boolean isChecked = sharedPreferences.getBoolean("sound", true);
mSoundSwitch.setChecked(isChecked);
}
}
现在,切换按钮与sharedPreferences
完美配合。但是,切换按钮时声音不会静音。
答案 0 :(得分:0)
private void mute() {
//mute audio
AudioManager amanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
}
public void unmute() {
//unmute audio
AudioManager amanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
}
这两个功能可以很好地处理音频mute
和unmute
。
您只需根据用户mute
拨打/切换到unmute
或preference
。