Android: accessing application preferences

by igor.ganapolsky

You can retrieve shared application preferences by using the getSharedPreferences() method of the application context:


SharedPreferences settings = getSharedPreferences("GamePrefs", MODE_PRIVATE);

SharedPreferences.Editor prefEditor = settings.edit();

prefEditor.putBoolean("HasCredits", true);

prefEditor.commit();

To retrieve preference settings, you simply retrieve SharedPreferences and read the values back out:


SharedPreferences settings = getSharedPreferences("GamePrefs", MODE_PRIVATE);

String userName = settings.getString("UserName", "Chippy Jr. (Default)");

~ SAMS TEACH YOURSELF ANDROID APPLICATION DEVELOPMENT IN 24 HOURS

Advertisements