안드로이드 개발시 간단한 정보를 저장할 수 있는 방식으로 클래스화 시켰음.

 

public enum PreferencesKey {
    INTRO,          
    PERMISSION,
    DEBUG,          //서버 전환용 플러그
    AUTO_LOGIN,
    CAMERA_URI      //시스템 카메라 이미지 저장 URI 저장용

}
/**
 * 참조사이트 : https://wuny-dev.tistory.com/20
 * Object 타입 추가
 */
public class PreferenceManager {

    public static final String PREFERENCES_NAME = "rebuild_preference";
    private static final String DEFAULT_VALUE_STRING = "";
    private static final boolean DEFAULT_VALUE_BOOLEAN = false;
    private static final int DEFAULT_VALUE_INT = -1;
    private static final long DEFAULT_VALUE_LONG = -1L;
    private static final float DEFAULT_VALUE_FLOAT = -1F;

    private static SharedPreferences getPreferences(@NonNull Context context) {
        return context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
    }

    /**
     * String 값 저장
     * @param context
     * @param key
     * @param value
     */
    public static void setString(Context context, @NonNull PreferencesKey key, String value) {
        SharedPreferences prefs = getPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(key.toString(), value);
        editor.apply();
    }

    /**
     * boolean 값 저장
     * @param context
     * @param key
     * @param value
     */
    public static void setBoolean(Context context, @NonNull PreferencesKey key, boolean value) {
        SharedPreferences prefs = getPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean(key.toString(), value);
        editor.apply();
    }

    /**
     * int 값 저장
     * @param context
     * @param key
     * @param value
     */
    public static void setInt(Context context, @NonNull PreferencesKey key, int value) {
        SharedPreferences prefs = getPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt(key.toString(), value);
        editor.apply();
    }

    /**
     * long 값 저장
     * @param context
     * @param key
     * @param value
     */
    public static void setLong(Context context, @NonNull PreferencesKey key, long value) {
        SharedPreferences prefs = getPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putLong(key.toString(), value);
        editor.apply();
    }

    /**
     * float 값 저장
     * @param context
     * @param key
     * @param value
     */
    public static void setFloat(Context context, @NonNull PreferencesKey key, float value) {
        SharedPreferences prefs = getPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putFloat(key.toString(), value);
        editor.apply();
    }

    /**
     * Object 값 저장
     * @param context
     * @param key
     * @param value
     */
    public  static void setObject(Context context, @NonNull PreferencesKey key, Object value) {
        Gson gson = new Gson();
        String stringValue = gson.toJson(value);
        SharedPreferences prefs = getPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(key.toString(), stringValue);
        editor.apply();
    }

    /**
     * String 값 로드
     * @param context
     * @param key
     * @return
     */
    public static String getString(Context context, @NonNull PreferencesKey key) {
        SharedPreferences prefs = getPreferences(context);
        String value = prefs.getString(key.toString(), DEFAULT_VALUE_STRING);
        return value;
    }

    /**
     * boolean 값 로드
     * @param context
     * @param key
     * @return
     */
    public static boolean getBoolean(Context context, @NonNull PreferencesKey key) {
        SharedPreferences prefs = getPreferences(context);
        boolean value = prefs.getBoolean(key.toString(), DEFAULT_VALUE_BOOLEAN);
        return value;
    }

    /**
     * int 값 로드
     * @param context
     * @param key
     * @return
     */
    public static int getInt(Context context, @NonNull PreferencesKey key) {
        SharedPreferences prefs = getPreferences(context);
        int value = prefs.getInt(key.toString(), DEFAULT_VALUE_INT);
        return value;
    }

    /**
     * long 값 로드
     * @param context
     * @param key
     * @return
     */
    public static long getLong(Context context, @NonNull PreferencesKey key) {
        SharedPreferences prefs = getPreferences(context);
        long value = prefs.getLong(key.toString(), DEFAULT_VALUE_LONG);
        return value;
    }

    /**
     * float 값 로드
     * @param context
     * @param key
     * @return
     */
    public static float getFloat(Context context, @NonNull PreferencesKey key) {
        SharedPreferences prefs = getPreferences(context);
        float value = prefs.getFloat(key.toString(), DEFAULT_VALUE_FLOAT);
        return value;
    }

    /**
     * Object 값 로드
     * @param context
     * @param key
     * @return
     */
    public static <T> Object getObject(Context context, @NonNull PreferencesKey key, Class<T> type) {
        SharedPreferences prefs = getPreferences(context);
        String stringValue = prefs.getString(key.toString(), DEFAULT_VALUE_STRING);
        Gson gson = new Gson();
        Object newObject = gson.fromJson(stringValue, type);
        return newObject;
    }
    /**
     * 키 값 삭제
     * @param context
     * @param key
     */
    public static void removeKey(Context context, @NonNull PreferencesKey key) {
        SharedPreferences prefs = getPreferences(context);
        SharedPreferences.Editor edit = prefs.edit();
        edit.remove(key.toString());
        edit.apply();
    }

    /**
     * 모든 저장 데이터 삭제
     * @param context
     */
    public static void clear(Context context) {
        SharedPreferences prefs = getPreferences(context);
        SharedPreferences.Editor edit = prefs.edit();
        edit.clear();
        edit.apply();
    }
}

 

 

[사용방법]

 

저장

PreferenceManager.setSting(this, PreferencesKey.DEBUG,  "운영");

불러오기

String debug = PreferenceManager.getString(this, PreferencesKey.DEBUG);

+ Recent posts