Skip to content

Instantly share code, notes, and snippets.

@vaibhav-jani
Created September 15, 2015 06:35
Show Gist options
  • Select an option

  • Save vaibhav-jani/6d1665e6c0fab534ed39 to your computer and use it in GitHub Desktop.

Select an option

Save vaibhav-jani/6d1665e6c0fab534ed39 to your computer and use it in GitHub Desktop.
Application preference wrapper class :
import android.content.Context;
import android.content.SharedPreferences;
public class AppPreferences {
private static String PREFERENCE_FILE_NAME = "application_preferences";
public static String IMEI = "imei";
private static SharedPreferences getSharedPreferences(Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences(PREFERENCE_FILE_NAME, 0);
return sharedPreferences;
}
private static SharedPreferences.Editor getSharedPreferencesEditor(Context context) {
return getSharedPreferences(context).edit();
}
public static boolean getBoolean(String key, boolean defaultValue, Context context) {
return getSharedPreferences(context).getBoolean(key, defaultValue);
}
public static void setBoolean(String key, boolean value, Context context) {
getSharedPreferencesEditor(context).putBoolean(key, value).commit();
}
public static int getInt(String key, int defaultValue, Context context) {
return getSharedPreferences(context).getInt(key, defaultValue);
}
public static void setInt(String key, int value, Context context) {
getSharedPreferencesEditor(context).putInt(key, value).commit();
}
public static String getString(String key, String defaultValue, Context context) {
return getSharedPreferences(context).getString(key, defaultValue);
}
public static void setString(String key, String value, Context context) {
getSharedPreferencesEditor(context).putString(key, value).commit();
}
public static float getFloat(String key, float defaultValue, Context context) {
return getSharedPreferences(context).getFloat(key, defaultValue);
}
public static void setFloat(String key, float value, Context context) {
getSharedPreferencesEditor(context).putFloat(key, value).commit();
}
public static long getLong(String key, long defaultValue, Context context) {
return getSharedPreferences(context).getLong(key, defaultValue);
}
public static void setLong(String key, long value, Context context) {
getSharedPreferencesEditor(context).putLong(key, value).commit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment