If you think the Android project visiting-card-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.matrix.visitingcard.util;
/*fromwww.java2s.com*/import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
/**
* Wrapper Singleton for SharedPrefs , Nothing fancy, keeps the code tidy
*
* @author yajnesh
*
*/publicclass SharedPrefs {
privatestatic SharedPreferences sp = null;
privatestatic Context context;
privatestaticfinal String SHARED_PREF_NAME = "com.matrix.visitingcard.util.sharedprefs";
privatestatic SharedPrefs instance = null;
publicstatic SharedPrefs getInstance(Context context) {
if (instance == null) {
SharedPrefs.context = context;
instance = new SharedPrefs();
}
return instance;
}
protected SharedPrefs() {
sp = context.getSharedPreferences(SHARED_PREF_NAME,
Context.MODE_PRIVATE);
}
/**
* Get Shared Preference value from specified key
*
* @param context
* The reference context
* @param key
* key to be searched
* @param defaultValue
* default value if key is not found
* @return If the key is present in SharedPrefs, returns the value with
* respect to key , or else returns defaultValue
*/public String getSharedPrefsValueString(String key, String defaultValue) {
return sp.getString(key, defaultValue);
}
/**
* Get Shared Preference value from specified key
*
* @param context
* The reference context
* @param key
* key to be searched
* @param defaultValue
* default value if key is not found
* @return If the key is present in SharedPrefs, returns the value with
* respect to key , or else returns defaultValue
*/publicboolean getSharedPrefsValueBool(String key, boolean defaultValue) {
return sp.getBoolean(key, defaultValue);
}
/**
* Get Shared Preference value from specified key
*
* @param context
* The reference context
* @param key
* key to be searched
* @param defaultValue
* default value if key is not found
* @return If the key is present in SharedPrefs, returns the value with
* respect to key , or else returns defaultValue
*/publicfloat getSharedPrefsValueFloat(String key, float defaultValue) {
return sp.getFloat(key, defaultValue);
}
publicint getSharedPrefsValueInt(String key, int defaultValue) {
return sp.getInt(key, defaultValue);
}
/**
* Save the key value pair in SharedPrefs
*
* @param context
* The reference context
* @param key
* Key of the value to be stored
* @param value
* value to be stored
*/publicvoid savePreferences(String key, String value) {
Editor editor = sp.edit();
editor.putString(key, value);
editor.commit();
}
/**
* Save the key value pair in SharedPrefs
*
* @param context
* The reference context
* @param key
* Key of the value to be stored
* @param value
* value to be stored
*/publicvoid savePreferences(String key, float value) {
Editor editor = sp.edit();
editor.putFloat(key, value);
editor.commit();
}
/**
* What do you think this does!
*/publicboolean destroy() {
return sp.edit().clear().commit();
}
}