RSS

Save and retrieve values from SharedPreferences – Android

19 Jan
public void savePreferences(Context context, String strKey, String strValue){
try {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(strKey, strValue);
editor.commit();
}
catch (Exception e) {
e.toString();
}
}

public Object getPreferences(Context context,String key,int preferenceDataType){
Object value = null;
try{
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
if (sharedPreferences.contains(key)){
switch(preferenceDataType){
case CommonEnvironment.PREFTYPE_BOOLEAN:
value =sharedPreferences.getBoolean(key, false);
break;
case CommonEnvironment.PREFTYPE_INT:
value = sharedPreferences.getInt(key, 0);
break;
case CommonEnvironment.PREFTYPE_STRING:
value = sharedPreferences.getString(key, "");
break;
}
}
}
catch(Exception e){
e.toString();
return null;
}
return value;
}
Advertisements
 
Leave a comment

Posted by on January 19, 2013 in Android Source Code

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: