2012-06-22 31 views
0

我的應用程序的啓動屏幕在啓動時播放一個mp3剪輯。Android - 在啓動屏幕上禁用聲音

我想讓用戶通過我的應用程序的設置菜單禁用/啓用聲音的選項。我該如何實現這一功能,以便應用程序在每次打開應用程序時都能記住用戶的偏好。

請參閱我的代碼下面的聲音。

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(); 

} 

回答

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(); 
+0

增加了 '承諾' 要不然也不會被保存... – RvdK