Skip to content

Instantly share code, notes, and snippets.

@hosseiniSeyRo
Last active December 7, 2019 14:01
Show Gist options
  • Save hosseiniSeyRo/2e761b1ff564aa21bfd7a4ebaf922ce9 to your computer and use it in GitHub Desktop.
Save hosseiniSeyRo/2e761b1ff564aa21bfd7a4ebaf922ce9 to your computer and use it in GitHub Desktop.
SharePrefUtils
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
public class SharePrefUtils {
//list of your keys here
public static final String IS_FIRST_LAUNCH = "is first launch";
private static SharedPreferences getSharePreference(Context context) {
// return context.getSharedPreferences("your_pref",Context.MODE_PRIVATE);
return PreferenceManager.getDefaultSharedPreferences(context);
}
private static SharedPreferences.Editor getEditor(Context context) {
return getSharePreference(context).edit();
}
public static boolean contain(Context context, String key) {
return getSharePreference(context).contains(key);
}
public static void remove(Context context, String key) {
getSharePreference(context).edit().remove(key).apply();
}
public static void removeAll(Context context) {
getSharePreference(context).edit().clear().apply();
}
public static void save(Context context, String key, Object value) {
if (value instanceof Integer) {
getEditor(context).putInt(key, (Integer) value).apply();
} else if (value instanceof String) {
getEditor(context).putString(key, value.toString()).apply();
} else if (value instanceof Boolean) {
getEditor(context).putBoolean(key, (Boolean) value).apply();
} else if (value instanceof Long) {
getEditor(context).putLong(key, (Long) value).apply();
} else if (value instanceof Float) {
getEditor(context).putFloat(key, (Float) value).apply();
} else if (value instanceof Double) {
getEditor(context).putLong(key, Double.doubleToRawLongBits((double) value)).apply();
}
}
public static Object get(Context context, String key, Object defaultValue) {
try {
if (defaultValue instanceof String) {
return getSharePreference(context).getString(key, defaultValue.toString());
} else if (defaultValue instanceof Integer) {
return getSharePreference(context).getInt(key, (Integer) defaultValue);
} else if (defaultValue instanceof Boolean) {
return getSharePreference(context).getBoolean(key, (Boolean) defaultValue);
} else if (defaultValue instanceof Long) {
return getSharePreference(context).getLong(key, (Long) defaultValue);
} else if (defaultValue instanceof Float) {
return getSharePreference(context).getFloat(key, (Float) defaultValue);
} else if (defaultValue instanceof Double) {
return Double.longBitsToDouble(getSharePreference(context).getLong(key, Double.doubleToLongBits((double) defaultValue)));
}
} catch (Exception e) {
Log.e("Exception", e.getMessage());
return defaultValue;
}
return defaultValue;
}
// //Save json object
// public static void saveObject(Context context, Key key, Object obj){
// Gson gson = new Gson();
// String json = gson.toJson(obj);
// getEditor(context).putString(key.name(), json).apply();
// }
//
// public static <T> T getObject(Context context, Key key, Class<T> clazz){
// Gson gson = new Gson();
// String json = getSharePreference(context).getString(key.name(), "");
// return gson.fromJson(json, clazz);
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment