欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android SharedPreferences存储的正确写法

程序员文章站 2023-11-20 22:48:52
sharedpreferences 特点 即便是android小白都知道的sharedpreferences的用法,这是保存数据最简便的方法,但是不处理好的话后期维护将是...

sharedpreferences 特点

即便是android小白都知道的sharedpreferences的用法,这是保存数据最简便的方法,但是不处理好的话后期维护将是一个巨大的坑。那么该如何处理好sharedpreferences才方便维护呢。先从它的特点开始入手吧。

  1. 通过context.getsharedpreferences()获取的sharedpreferences是一个单例
  2. sharedpreferences.edit()每次都会创建一个新的编辑对象,commit()之前一切改动都无效

sharedpreferences的具体实现并不在公开api内,创建方法只有通过context获取。具体实现我们不去研究,通过log打印每次获取的对象都是同一个,同样的方法可知道每次edit()都会得到不同的对象。

难以维护的 sharedpreferences

sharedpreference userpreferences = context.getsharedpreferences("userinfo", context.mode_private);
userpreferences.edit()
  .putstring("id", "myid")
  .putstring("name", "myname")
  .putstring("head", "myhead")
  .commit();
log.e("id", userpreferences.getstring("id",""));
log.e("name", userpreferences.getstring("name",""));
log.e("head", userpreferences.getstring("head",""));

如上一段sharedpreferences保存和读取的方法非常简便,正因此才不易维护,可能因为写错key而找不出问题所在,当然一般我们都会把key写成静态常量。但随着项目增大,可能需要多处修改用户信息,每次都要一个个put(), 除了写着厌烦看着难受,当遇到需要增加或减少字段时也非常不便。

sharedpreferences 该这么写

如果能保留原有写法,又能规范方法那是最好不过了,看下sharedpreferences的接口方法并不多,很容易就写个包装类封装下。

import android.content.sharedpreferences;
import android.support.annotation.nullable;

import java.util.map;
import java.util.set;

/**
 * sharedpreferences 包装类
 * created by wittytutu on 17-3-29.
 */

public class preferenceswrapper implements sharedpreferences {

  protected sharedpreferences mpreferences;

  public preferenceswrapper(sharedpreferences preferences) {
    mpreferences = preferences;
  }

  @override
  public map getall() {
    return mpreferences.getall();
  }

  @nullable
  @override
  public string getstring(string key, @nullable string defvalue) {
    return mpreferences.getstring(key, defvalue);
  }

  @nullable
  @override
  public set getstringset(string key, @nullable set defvalues) {
    return mpreferences.getstringset(key, defvalues);
  }

  @override
  public int getint(string key, int defvalue) {
    return mpreferences.getint(key, defvalue);
  }

  @override
  public long getlong(string key, long defvalue) {
    return mpreferences.getlong(key, defvalue);
  }

  @override
  public float getfloat(string key, float defvalue) {
    return mpreferences.getfloat(key, defvalue);
  }

  @override
  public boolean getboolean(string key, boolean defvalue) {
    return mpreferences.getboolean(key, defvalue);
  }

  @override
  public boolean contains(string key) {
    return mpreferences.contains(key);
  }

  @override
  public editorwrapper edit() {
    return new editorwrapper(mpreferences.edit());
  }

  @override
  public void registeronsharedpreferencechangelistener(onsharedpreferencechangelistener listener) {
    mpreferences.registeronsharedpreferencechangelistener(listener);
  }

  @override
  public void unregisteronsharedpreferencechangelistener(onsharedpreferencechangelistener listener) {
    mpreferences.unregisteronsharedpreferencechangelistener(listener);
  }

  public static class editorwrapper implements editor {
    protected editor meditor;

    public editorwrapper(editor editor) {
      meditor = editor;
    }

    @override
    public editorwrapper putstring(string key, @nullable string value) {
      meditor.putstring(key, value);
      return this;
    }

    @override
    public editorwrapper putstringset(string key, @nullable set values) {
      meditor.putstringset(key, values);
      return this;
    }

    @override
    public editorwrapper putint(string key, int value) {
      meditor.putint(key, value);
      return this;
    }

    @override
    public editorwrapper putlong(string key, long value) {
      meditor.putlong(key, value);
      return this;
    }

    @override
    public editorwrapper putfloat(string key, float value) {
      meditor.putfloat(key, value);
      return this;
    }

    @override
    public editorwrapper putboolean(string key, boolean value) {
      meditor.putboolean(key, value);
      return this;
    }

    @override
    public editorwrapper remove(string key) {
      meditor.remove(key);
      return this;
    }

    @override
    public editorwrapper clear() {
      meditor.clear();
      return this;
    }

    @override
    public boolean commit() {
      return meditor.commit();
    }

    @override
    public void apply() {
      meditor.apply();
    }
  }
}

接下来的使用就简单了

public class userinfopreferences extends preferenceswrapper {
  private static final string name = "userinfo";

  public static final key_id = "id"
  public static final key_name = "name";
  public static final key_head = "head";
  public static final key_sex = "sex";

  private static volatile userinfopreferences mpreferences;

  public static userinfopreferences getinstance(context context){
    if(mpreferences == null) {
      synchronized (userinfopreferences.class){
        if(mpreferences == null) {
          sharedpreferences preferences = context.getapplicationctontext().getshaedpreferences(name, context.mode_private);
          mpreferences = new userinfopreferences(preferences);
        }
      }
    }
    return mpreferences;
  }

  public userinfopreferences(sharedpreferences preferences) {
    super(preferences);
  }

  // 获取用户信息
  public userinfo getuserinfo(){
    string id = mpreferences.getstring(key_id, "");
    string name = mpreferences.getstring(key_name, "");
    string head = mpreferences.getstring(key_head, "");
    return new userinfo(id, name, head);
  }

  @override
  public editor edit(){
    return new editor(mpreferences.edit());
  }

  public static class editor extends editorwrapper {

    public editor(sharedpreferences.editor editor) {
      super(editor);
    }

    // 保存用户信息
    public editor putuserinfo(string id, string name, string head) {

      meditor.putstring(key_id, id)
          .putstring(key_ame, name)
          .putstring(key_head, head);
      return this;
    }

    // 清空用户信息
    public editor clearuserinfo() {

      meditor.remove(key_id)
          .remove(key_name)
          .remove(key_head);
      return this;
    }
  }
}

调用即方便扩展,也保留了原有方法。

userinfopreferences preferences = userinfopreferences.getinstance(context);
preferences.edit()
  .clearuserinfo()
  .putuserinfo("myid", "myname", "myhead")
  .putint(key_sex, 0)
  .commit();
userinfo userinfo = preferences.getuserinfo();
int sex = preferences.getint(key_sex, 0);

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!