android SharedPreference封装为工具类

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

public class SharedPreferencesHelperUtil {

    private final String TAG = this.getClass().getSimpleName();

    // file name
    private static final String PREFERENCES_FILE_NAME = "androidpn_demo";

    // instance
    private static SharedPreferencesHelperUtil INSTANCE = null;

    // shared preference
    private SharedPreferences sp;
    // shared preference editor
    private SharedPreferences.Editor editor;

    // is initialized
    private boolean initialized = false;

    public static final synchronized SharedPreferencesHelperUtil getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new SharedPreferencesHelperUtil();
        }
        return INSTANCE;
    }

    public void init(Context context) {
        this.sp = context.getSharedPreferences(PREFERENCES_FILE_NAME,
                Context.MODE_PRIVATE);
        this.editor = sp.edit();
        initialized = true;
    }

    public boolean isInitialized() {
        return initialized;
    }

    /**
     * Push string value
     * 
     * @param key
     * @param value
     */
    public void putStringValue(String key, String value) {

        Log.d(TAG, "--------putStringValue----------");
        Log.d(TAG, "key: " + key);
        Log.d(TAG, "value: " + value);

        editor = sp.edit();
        editor.putString(key, value);
        editor.commit();
    }

    /**
     * Get string value by key
     * 
     * @param key
     * @param defaultValue
     * 
     * @return
     */
    public String getStringValue(String key, String defaultValue) {
        return sp.getString(key, defaultValue);
    }

    /**
     * Push integer value
     * 
     * @param key
     * @param value
     */
    public void putIntValue(String key, int value) {
        editor = sp.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    /**
     * Get integer value by key
     * 
     * @param key
     * @param defaultValue
     * 
     * @return
     */
    public int getIntValue(String key, int defaultValue) {
        return sp.getInt(key, defaultValue);
    }

    /**
     * Push long value
     * 
     * @param key
     * @param value
     */
    public void putLongValue(String key, long value) {
        editor = sp.edit();
        editor.putLong(key, value);
        editor.commit();
    }

    /**
     * Get long value by key
     * 
     * @param key
     * @param defaultValue
     * 
     * @return
     */
    public long getLongValue(String key, long defaultValue) {
        return sp.getLong(key, defaultValue);
    }

    /**
     * Push boolean value
     * 
     * @param key
     * @param value
     */
    public void putBooleanValue(String key, boolean value) {
        editor = sp.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    /**
     * 
     * @param key
     * @return
     */
    public boolean contains(String key) {
        return sp.contains(key);
    }

    /**
     * 
     * @param key
     */
    public void remove(String key) {

        Log.d(TAG, "--------remove----------");

        Log.d(TAG, "key: " + key);

        editor = sp.edit();
        editor.remove(key);
        editor.commit();
    }

    /**
     * 
     */
    public void clear() {
        editor = sp.edit();
        editor.clear();
        editor.commit();
    }

    /**
     * Get boolean value by key
     * 
     * @param key
     * @param defaultValue
     * 
     * @return
     */
    public boolean getBooleanValue(String key, boolean defaultValue) {
        return sp.getBoolean(key, defaultValue);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bjxiaxueliang

您的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值