private static void assignByType( SharedPreferences.Editor editor, String key, Object value) throws IllegalArgumentException { if (value instanceof Integer) { editor.putInt(key, (Integer)value); } else if (value instanceof Long) { editor.putLong(key, (Long)value); } else if (value instanceof Float) { editor.putFloat(key, (Float)value); } else if (value instanceof Boolean) { editor.putBoolean(key, (Boolean)value); } else if (value instanceof String) { editor.putString(key, (String)value); } else if (value instanceof Set) { putStringSet(editor, key, (Set<String>)value); } else { throw new IllegalArgumentException("Unsupported type=" + value.getClass().getName()); } }
public void storeIntArray(int[] array){ SharedPreferences.Editor edit= mContext.getSharedPreferences("NAME", Context.MODE_PRIVATE).edit(); edit.putInt("Count", array.length); int count = 0; for (int i: array){ edit.putInt("IntValue_" + count++, i); } edit.commit(); } public int[] getFromPrefs(){ int[] ret; SharedPreferences prefs = mContext.getSharedPreferences("NAME", Context.MODE_PRIVATE); int count = prefs.getInt("Count", 0); ret = new int[count]; for (int i = 0; i < count; i++){ ret[i] = prefs.getInt("IntValue_"+ i, i); } return ret; }
synchronized public static void incrementChargingReminderCount(Context context) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); int chargingReminders = prefs.getInt(KEY_CHARGING_REMINDER_COUNT, DEFAULT_COUNT); SharedPreferences.Editor editor = prefs.edit(); editor.putInt(KEY_CHARGING_REMINDER_COUNT, ++chargingReminders); editor.apply(); }
public boolean saveArray(String[] array, String arrayName, Context mContext) { SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0); SharedPreferences.Editor editor = prefs.edit(); editor.putInt(arrayName +"_size", array.length); for(int i=0;i<array.length;i++) editor.putString(arrayName + "_" + i, array[i]); return editor.commit(); }
synchronized public static void incrementChargingReminderCount(Context context) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); int chargingReminders = prefs.getInt(KEY_CHARGING_REMINDER_COUNT, DEFAULT_COUNT); SharedPreferences.Editor editor = prefs.edit(); editor.putInt(KEY_CHARGING_REMINDER_COUNT, ++chargingReminders); editor.apply(); }