투케이2K

6. (AndroidStudio/android/java) SharedPreferences 프리퍼런스 사용해 string, int, boolean 값 저장 실시 - 작은 DB 사용 본문

Android

6. (AndroidStudio/android/java) SharedPreferences 프리퍼런스 사용해 string, int, boolean 값 저장 실시 - 작은 DB 사용

투케이2K 2021. 1. 20. 15:17
반응형

/* =========================== */

[ 개발 환경 설정 ]

개발 툴 : AndroidStudio

개발 언어 : java

/* =========================== */

/* =========================== */

[소스 코드]

 

package kr.co.app.manager;

import android.content.Context;
import android.content.SharedPreferences;

//========== 모바일상에서 데이터를 저장하기 위한 클래스(영구적) ==========
public class S_Preference {

	//========== [사용 설명] ==========
	/** [String 데이터 저장]
	 S_Preference.setString(getApplication(), "Key_Name", "Data_kwon"); //특정 데이터 저장한다
	 S_Preference.getString(getApplication(), "Key_Name"); //저장된 특정 데이터 불러온다
	 */

	/** [Int 데이터 저장]
	 S_Preference.setInt(getApplication(), "Key_Age", 28); //특정 데이터 저장한다
	 S_Preference.getInt(getApplication(), "Key_Age"); //저장된 특정 데이터 불러온다
	 */

	/** [Boolean 데이터 저장]
	 S_Preference.setBoolean(getApplication(), "Key_Sex", true); //특정 데이터 저장한다
	 S_Preference.getBoolean(getApplication(), "Key_Sex"); //저장된 특정 데이터 불러온다
	 */

	/** [특정 데이터 삭제]
	 S_Preference.removeKey(getApplication(), "Key_Name"); //특정 데이터 삭제한다
	 */

	/** [전체 데이터 삭제]
	 S_Preference.clear(getApplication()); //전체 데이터 삭제한다
	 */

	//========== [전역 변수 선언 ==========
	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(Context context) {
		return context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
	}

	//========== [String 값 저장] ==========
	/**
	 * String 값 저장
	 */
	public static void setString(Context context, String key, String value) {
		SharedPreferences prefs = getPreferences(context);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(key, value);
		editor.commit();
		editor.apply();
	}

	//========== [boolean 값 저장] ==========
	/**
	 * boolean 값 저장
	 */
	public static void setBoolean(Context context, String key, boolean value) {
		SharedPreferences prefs = getPreferences(context);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean(key, value);
		editor.commit();
		editor.apply();
	}

	//========== [int 값 저장] ==========
	/**
	 * int 값 저장
	 */
	public static void setInt(Context context, String key, int value) {
		SharedPreferences prefs = getPreferences(context);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putInt(key, value);
		editor.commit();
		editor.apply();
	}

	//========== [String 값 호출] ==========
	/**
	 * String 값 로드
	 */
	public static String getString(Context context, String key) {
		SharedPreferences prefs = getPreferences(context);
		String value = prefs.getString(key, DEFAULT_VALUE_STRING);
		return value;
	}

	//========== [boolean 값 호출] ==========
	/**
	 * boolean 값 로드
	 */
	public static boolean getBoolean(Context context, String key) {
		SharedPreferences prefs = getPreferences(context);
		boolean value = prefs.getBoolean(key, DEFAULT_VALUE_BOOLEAN);
		return value;
	}

	//========== [int 값 호출] ==========
	/**
	 * int 값 로드
	 */
	public static int getInt(Context context, String key) {
		SharedPreferences prefs = getPreferences(context);
		int value = prefs.getInt(key, DEFAULT_VALUE_INT);
		return value;
	}

	//========== [특정 key 값 삭제] ==========
	/**
	 * 키 값 삭제
	 */
	public static void removeKey(Context context, String key) {
		SharedPreferences prefs = getPreferences(context);
		SharedPreferences.Editor edit = prefs.edit();
		edit.remove(key);
		edit.commit();
	}

	//========== [전체 key 값 삭제] ==========
	/**
	 * 모든 저장 데이터 삭제
	 */
	public static void clear(Context context) {
		SharedPreferences prefs = getPreferences(context);
		SharedPreferences.Editor edit = prefs.edit();
		edit.clear();
		edit.commit();
	}

}//TODO 클래스 종료

/* =========================== */

/* =========================== */

[요약 설명]

/** [String 데이터 저장]

S_Preference.setString(getApplication(), "Key_Name", "Data_kwon"); //특정 데이터 저장한다

S_Preference.getString(getApplication(), "Key_Name"); //저장된 특정 데이터 불러온다

*/

/** [Int 데이터 저장]

S_Preference.setInt(getApplication(), "Key_Age", 28); //특정 데이터 저장한다

S_Preference.getInt(getApplication(), "Key_Age"); //저장된 특정 데이터 불러온다

*/

/** [Boolean 데이터 저장]

S_Preference.setBoolean(getApplication(), "Key_Sex", true); //특정 데이터 저장한다

S_Preference.getBoolean(getApplication(), "Key_Sex"); //저장된 특정 데이터 불러온다

*/

/** [특정 데이터 삭제]

S_Preference.removeKey(getApplication(), "Key_Name"); //특정 데이터 삭제한다

*/

/** [전체 데이터 삭제]

S_Preference.clear(getApplication()); //전체 데이터 삭제한다

*/

/* =========================== */

반응형
Comments