RSS

Android Accessing Preferences in an Actitvity

04 Jul

I though I could somehow give the user an option of muting the sound in the splash screen(Click here to check out the sourcecode). So in here i set up the preferences, Now we want to read those preferences and use them accordingly. I modified the splash screen such that it will only play music if the user has set it to.
So first you have to access the Preferences:

SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

Now well check the value of the checkbox and store it in boolean variable playMusic

boolean playMusic = getPrefs.getBoolean("sound", true);

Now to the block of code should look like:

SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
		boolean playMusic = getPrefs.getBoolean("sound", true);
		
		if(playMusic)
			song.start();
 
1 Comment

Posted by on July 4, 2012 in Android, Android tutorials, Code

 

Tags:

One response to “Android Accessing Preferences in an Actitvity

  1. Mauricio Ponce (@th4n0)

    April 22, 2013 at 3:07 pm

    thanks, it was very useful

     

Leave a comment