SharedPreferences Utils(경량 스토리지 도구 클래스)

용도: 태그 데이터 저장 및 정보 설정 기능: 1.저장 문자열:saveString 2.반환 문자열: getString 3.부울 저장: saveBoolean 4.부울로 돌아가기:getBoolean 5.int:saveInt 6. 저장int:getInt 7 로 돌아가기float 저장:saveFloat 8.float:getFloat 9.키가 이미 있는지 확인하기: contains
public class SharedPreferencesUtils {

    public static final String SP_NAME = "config";
    private static SharedPreferences sp;

    /**
     *      
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveString(Context context, String key, String value) {
        if (sp == null){
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
        sp.edit().putString(key, value).commit();
    }

    /**
     *      
     *
     * @param context
     * @param key
     * @param defValue    
     * @return
     */
    public static String getString(Context context, String key, String defValue) {
        if (sp == null){
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
        return sp.getString(key, defValue);
    }

    /**
     *     
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveBoolean(Context context, String key, boolean value) {
        if (sp == null){
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
        sp.edit().putBoolean(key, value).commit();
    }

    /**
     *     
     *
     * @param context
     * @param key
     * @param defValue    
     * @return
     */
    public static boolean getBoolean(Context context, String key, boolean defValue) {
        if (sp == null){
            sp = context.getSharedPreferences(SP_NAME, 0);
        }

        return sp.getBoolean(key, defValue);
    }

    /**
     *   int
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveInt(Context context, String key, int value) {
        if (sp == null){
            sp = context.getSharedPreferences(SP_NAME, 0);
        }

        sp.edit().putInt(key, value).commit();
    }
    /**
     *   int
     *
     * @param context
     * @param key
     * @param defValue    
     * @return
     */
    public static int getInt(Context context, String key, int defValue) {
        if (sp == null){
            sp = context.getSharedPreferences(SP_NAME, 0);
        }

        return sp.getInt(key, defValue);
    }
    /**
     *   float
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveFloat(Context context, String key, float value) {
        if (sp == null){
            sp = context.getSharedPreferences(SP_NAME, 0);
        }

        sp.edit().putFloat(key, value).commit();
    }
    /**
     *   float
     *
     * @param context
     * @param key
     * @param defValue    
     * @return
     */
    public static float getFloat(Context context, String key, float defValue) {
        if (sp == null){
            sp = context.getSharedPreferences(SP_NAME, 0);
        }

        return sp.getFloat(key, defValue);
    }

    /**
     *     key      
     *
     * @param context
     * @param key
     * @return
     */
    public static boolean contains(Context context, String key) {
        if (sp == null){
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
        return sp.contains(key);
    }

}

좋은 웹페이지 즐겨찾기