jeudi 13 août 2015

Mute audio in preference activity

I'm fairly new with Java development and I started building a simple Android app. My MainActivity creates a SurfaceView called 'GameView' where I've created bouncing balls. An options menu is also created (in my MainActivity) which has a 'Settings' item. When tapping on the settings button, a new PreferenceActivity starts called SettingsActivity. In the settings menu I have to simple checkboxes that have to control the background music and sound effects (mute/unmute).

My checkboxes in the settings.xml:

<CheckBoxPreference
     android:summary="Turn music on or off"
     android:defaultValue="true"
     android:title="Music"
     android:key="musicPref" 
/>
<CheckBoxPreference
     android:summary="Turn sounds on or off"
     android:defaultValue="true"
     android:title="Sounds"
     android:key="soundPref" 
/> 

I've used the following code in my public GameView function in order to get the value of the checkboxes:

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
musicPlaying = sharedPrefs.getBoolean("musicPref", true );
soundPlaying = sharedPrefs.getBoolean("soundPref", true );

I guess I should have a OnClickListener in my settings activity, but how do I pass it to my GameView?




Aucun commentaire:

Enregistrer un commentaire