我的应用程序有一个启动屏幕,可以在启动时播放 mp3 剪辑。
我想为用户提供通过我的应用程序的设置菜单禁用/启用声音的选项。我怎样才能实现这个,以便应用程序记住用户每次打开应用程序时的偏好。
请参阅下面我的代码以了解声音。
public class Splash extends SherlockActivity {
SoundPool sp;
int explosion = 0;
MediaPlayer mp;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.splash);
sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
explosion = sp.load(this, R.raw.soundfile, 1);
Thread timer = new Thread() {
public void run() {
try {
sleep(5000);
if (explosion != 0)
sp.play(explosion, 1, 1, 0, 0, 1);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent openMenu = new Intent(
"ttj.android.t3w.STARTINGPOINT");
startActivity(openMenu);
}
}
};
timer.start();
}
最佳答案
使用 SharedPreferences 来存储和检索它:http://developer.android.com/reference/android/content/SharedPreferences.html
示例:http://developer.android.com/guide/topics/data/data-storage.html#pref
public static final String PREFS_NAME = "MyPrefsFile";
//retrieve
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("silentMode", false);
//use silent
//store
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", mSilentMode);
editor.commit();
关于java - Android - 禁用启动屏幕上的声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11155151/