美文网首页
android中的SharePerference怎么用简单

android中的SharePerference怎么用简单

作者: 杨hai | 来源:发表于2017-09-14 09:42 被阅读78次
java 版本

android几种缓存数据的方法之一的SharePerference的使用,需要传递好几个参数,为了方便使用,我做了如下包装:

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

import cn.dslc.dslc.common.constansts.AppConstant;

/**
 * Created by yh on 16/6/13.
 */
public class ShareUtils {


    private static Context mContext;
    private static SharedPreferences sharedPreferences;
    private static SharedPreferences.Editor editor;


    public static void init(Context context) {
        mContext = context;
        sharedPreferences = mContext.getSharedPreferences(AppConstant.ShareName.SHARE_NAME, Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }

    public static void saveString(String key, String value) {
        editor.putString(key, value);
        editor.commit();
    }

    public static String getString(String key, String normal) {
        return sharedPreferences.getString(key, normal);
    }


    public static String getString(String key) {
        return sharedPreferences.getString(key, "");
    }


    public static void saveInteger(String key, int value) {
        editor.putInt(key, value);
        editor.commit();
    }

    public static int getInteger(String key, int normal) {
        return sharedPreferences.getInt(key, normal);
    }

    public static int getInteger(String key) {
        return sharedPreferences.getInt(key, 0);
    }


    public static Boolean getBoolean(String key) {
        return sharedPreferences.getBoolean(key, true);
    }

    public static Boolean getBoolean(String key, boolean b) {
        return sharedPreferences.getBoolean(key, b);
    }

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

    /**
     * 移除key
     *
     * @param key
     */
    public static void remove(String key) {
        editor.remove(key).commit();
    }


}

在使用前,需要在application中init一下,在其他使用的地方如下方式:

ShareUtils.saveString("key","value");

kotlin 版本


import android.content.Context
import manage.ad.yiba.com.admanage.App
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

/**
 * Created by yh on 8/25/17.
 */
class Preference<T>(context: Context = App.instance, val name: String?, val default: T) : ReadWriteProperty<Any?, T> {

    val prefs by lazy {
       context.getSharedPreferences("yiba",Context.MODE_PRIVATE)
    }
    override fun getValue(thisRef: Any?, property: KProperty<*>): T = with(prefs){
        when (default) {
            is Long -> {
                getLong(name, default)
            }
            is String -> {
                getString(name, default)
            }
            is Float -> {
                getFloat(name, default)
            }
            is Int -> {
                getInt(name, default)
            }
            is Boolean -> {
                getBoolean(name, default)
            }
            else -> {
                throw IllegalArgumentException("This type can be saved into Preferences")
            }
        } as T
    }

    override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) = with(prefs.edit()){
        when (value) {
            is Long -> putLong(name, value)
            is String -> putString(name, value)
            is Float -> putFloat(name, value)
            is Int -> putInt(name, value)
            is Boolean -> putBoolean(name, value)
            else -> {
                throw  IllegalArgumentException("存储的类型不存在,请确认储存类型")
            }
        }.apply()
    }

}

相关文章

网友评论

      本文标题:android中的SharePerference怎么用简单

      本文链接:https://www.haomeiwen.com/subject/jyntrxtx.html