0

我的应用程序有一个启动画面,它在启动时播放 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();

}
4

1 回答 1

2

使用 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();
于 2012-06-22T11:17:47.130 回答