问题
Is it possible to turn off all sounds of just one app and to save the choice until the next start of the app?
So I have a ToggleButton at the StartActivity and by pressing this Button, all sounds of the app should be deactivated, but sounds of other apps should still be working.
I tried it with Preferences, using the SharedPreferences class, an xml file and so on, like in my book. But going through the introduction i realized, that the Preferences are just a way to save data permanent. So I would have to include for example on every Button with a sound an if-clause that checks if this or that saved preference for sound on/off is true/false and then activates/deactivates the sound?
I thought that it could be easier?
回答1:
Actually, if you want to disable a sound in your application - you can just don't play it. You can store the flags for each sound you want to play in the SharedPreferences
and check it every time you want to play a sound. It's really easy!
回答2:
You can probably write your own MyButton class that extends Button and override onClick.
回答3:
Declare a string
String strSound;
enter this line in your shared preference settings
strSound = prefs.getString("sound", "on");
enter this line in your
onActivityResult
strSound = prefs.getString("sound", "on");
put this lines in your media player which is used to give sounds for your application
private void playSound() { mp = MediaPlayer.create(MyActivity.this, R.raw."your_soundfile_name"); mp.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { mp.release(); } }); if(strSound.equals("on")) mp.start();
now put this lines on your toggle button activity
your_togglebutton_id.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { prefs = getSharedPreferences(prefName, MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); if (isChecked) { editor.putString("sound", "on"); editor.commit(); } else { editor.putString("sound", "off"); editor.commit(); } } }
来源:https://stackoverflow.com/questions/7236678/disable-sound-just-for-one-application