View RSS Feed

Android Developer

How to access Android Application Preferences?

Rate this Entry
by , 02-11-2012 at 11:51 AM (1436 Views)
You can use getSharedPreferences() method of your application context to get your application preferences. Also SharedPreferences class support saving of application data like configuration settings. You need to assign each SharedPreferences object a different and unique name that enables you to arrange preference values in different categories and can store all preferences in on large unnamed set.

For example, if you are interested in storing user name and game statistics like how many credited are left to play the game. Below code creates a set of preferences which are shared.

Java Code: This is the code to access Android Application Preferences
public class Calc extends Activity {
    public static final String PREFS_NAME = "MyPrefsFile";

    @Override
    protected void onCreate(Bundle state){
       super.onCreate(state);
       . . .

       // Restore preferences
       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
       boolean silent = settings.getBoolean("silentMode", false);
       setSilent(silent);
    }

    @Override
    protected void onStop(){
       super.onStop();

      // We need an Editor object to make preference changes.
      // All objects are from android.context.Context
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putBoolean("silentMode", mSilentMode);

      // Commit the edits!
      editor.commit();
    }
}

Submit "How to access Android Application Preferences?" to Facebook Submit "How to access Android Application Preferences?" to Digg Submit "How to access Android Application Preferences?" to del.icio.us Submit "How to access Android Application Preferences?" to StumbleUpon Submit "How to access Android Application Preferences?" to Google

Comments