728x90

안드로이드에서 Java Singleton 패턴으로 SharedPreferences 처리를 하는 코드를 적어둔다.


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

public class PrefsHelper {
    private static Context mContext;
    private static SharedPreferences prefs;
    private static SharedPreferences.Editor editor;
    private static PrefsHelper prefmodule = null;
    public static final String PREF_NAME ="pref";

    public static final String BaudRate = "BaudRate";
    public static final String DataBit = "DataBit";
    public static final String StopBit = "StopBit";
    public static final String Parity ="Parity";

    public static PrefsHelper getInstance(Context context) {
        mContext = context;

        if (prefmodule == null) {
            prefmodule = new PrefsHelper();
        }
        if(prefs==null){
            prefs = mContext.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
            editor = prefs.edit();
        }
        return prefmodule;
    }

    public static String read(String key, String defValue) {
        return prefs.getString(key, defValue);
    }

    public static void write(String key, String value) {
        SharedPreferences.Editor prefsEditor = prefs.edit();
        prefsEditor.putString(key, value);
        prefsEditor.commit();
    }

    public static boolean read(String key, boolean defValue) {
        return prefs.getBoolean(key, defValue);
    }

    public static void write(String key, boolean value) {
        SharedPreferences.Editor prefsEditor = prefs.edit();
        prefsEditor.putBoolean(key, value);
        prefsEditor.commit();
    }

    public static Integer read(String key, int defValue) {
        return prefs.getInt(key, defValue);
    }

    public static void write(String key, Integer value) {
        SharedPreferences.Editor prefsEditor = prefs.edit();
        prefsEditor.putInt(key, value).commit();
    }
}


사용법 write

private void writePreferences(String baudRate, String dataBit, String stopBit, int parity){
    PrefsHelper.getInstance(this).write(PrefsHelper.BaudRate, baudRate);
    PrefsHelper.getInstance(this).write(PrefsHelper.DataBit, dataBit);
    PrefsHelper.getInstance(this).write(PrefsHelper.StopBit, stopBit);
    PrefsHelper.getInstance(this).write(PrefsHelper.Parity, String.valueOf(parity));
}


읽기

String BaudRate = PrefsHelper.getInstance(mContext).read(PrefsHelper.BaudRate,null);
String DataBit = PrefsHelper.getInstance(mContext).read(PrefsHelper.DataBit,null);
String StopBit = PrefsHelper.getInstance(mContext).read(PrefsHelper.StopBit,null);
String Parity = PrefsHelper.getInstance(mContext).read(PrefsHelper.Parity,null);




다른 예제

이 예제를 활용하면 여러모로 편하다.

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

public class PrefsHelper {
    public static final String PREFERENCE_NAME="pref";
    private Context mContext;
    private static SharedPreferences prefs;
    private static SharedPreferences.Editor prefsEditor;
    private static PrefsHelper instance;

    public static synchronized PrefsHelper init(Context context){
        if(instance == null)
            instance = new PrefsHelper(context);
        return instance;
    }

    private PrefsHelper(Context context) {
        mContext = context;
        prefs = mContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE );
        prefsEditor = prefs.edit();
    }

    public static String read(String key, String defValue) {
        return prefs.getString(key, defValue);
    }

    public static void write(String key, String value) {
        prefsEditor.putString(key, value);
        prefsEditor.commit();
    }

    public static Integer read(String key, int defValue) {
        return prefs.getInt(key, defValue);
    }

    public static void write(String key, Integer value) {
        prefsEditor.putInt(key, value).commit();
    }

    public static boolean read(String key, boolean defValue) {
        return prefs.getBoolean(key, defValue);
    }

    public static void write(String key, boolean value) {
        prefsEditor.putBoolean(key, value);
        prefsEditor.commit();
    }
}


사용방법

PrefsHelper.init(getApplicationContext()); // 한번만 실행하면 된다.


// 쓰기

PrefsHelper.write("userid",userID);
PrefsHelper.write("userpw",userPW);


// 읽기

userID = PrefsHelper.read("userid","");
userPW = PrefsHelper.read("userpw","");

728x90
블로그 이미지

Link2Me

,