SharedPreferences工具类SPHepler

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SPHepler {
    private static Context context;
    private static SPHepler util;

    public static SPHepler getInstance(Context mcontext) {
        context=mcontext;
        if (util == null) {
            util = new SPHepler();

        }
        return util;

    }

    private SPHepler() {
        super();
    }

    private String name = context.getPackageName();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getInt(String key) {

        SharedPreferences sp = context.getSharedPreferences(name,
                Context.MODE_PRIVATE);

        return sp.getInt(key, 0);
    }

    public void setInt(String key, int num) {

        SharedPreferences sp = context.getSharedPreferences(name,
                Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putInt(key, num);
        editor.commit();

    }

    public String getString(String key) {

        SharedPreferences sp = context.getSharedPreferences(name,
                Context.MODE_PRIVATE);

        return sp.getString(key, "");
    }

    public void setString(String key, String str) {

        SharedPreferences sp = context.getSharedPreferences(name,
                Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putString(key, str);

        editor.commit();

    }
}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:PHP生成一段15位的随机字符串

下一篇:python实现ftp上传文件