当前位置: 移动技术网 > 移动技术>移动开发>Android > 10.Android-SharedPreferences使用

10.Android-SharedPreferences使用

2020年01月10日  | 移动技术网移动技术  | 我要评论

1.sharedpreferences介绍

sharedpreferences,它是一个轻量级的配置文件类,用于保存软件配置参数. 采用xml文件形式存储在/data/data/包名/shared_prefs/

优点在于:

  • 轻量级,以键值对的方式进行存储,使用方便,易于理解
  • 程序卸载后会也会一并被清除,不会残留信息

缺点在于:

  • 不支持跨多个进程使用
  • 键值数据如果过多,会导致界面卡顿


2.如何获取sharedpreferences

2.1 通过context .getsharedpreferences()方法

public sharedpreferences getsharedpreferences(string name, int mode);
//name:指定文件名称
//mode: 可以填入mode_private(文件只能由调用应用程序访问)、
//mode_append(追加内容模式)、mode_world_readable(文件支持所有人可读)、mode_world_writeable(文件支持所有人可写)

 

2.2 通过activity 类中的 getpreferences()方法

public sharedpreferences getpreferences(int mode);
//mode: 可以填入mode_private(文件只能由调用应用程序访问)、mode_append(追加内容模式)、mode_world_readable(文件支持所有人可读)、mode_world_writeable(文件支持所有人可写)
//生成的配置文件名字会默认将当前活动的类名作为文件名.比如在mainactivity类下使用的,则会生成mainactivity.xml

 

2.3 通过preferencemanager中的静态public getdefaultsharedpreferences()方法

public static sharedpreferences getdefaultsharedpreferences(context context);
//mode会自动设置为mode_private(文件只能由调用应用程序访问)
//生成的配置文件名字会默认将包名作为文件名.

 

ps:后面示例会演示。

 

3.如何通过sharedpreferences来读写配置参数

通过下面方法来进行读操作:

 

上面的第二个参数defvalue:表示默认值,如果key值未找到,则将defvalue默认值返回过来

 

通过editor edit()成员方法来获取editor类来实现写操作,如下所示:

 

editor类下的常用方法有:

editor putint(string key, int value); //向key键里写入int类型的值
editor putstring(string key, string value);   //向key键里写入string类型的值
editor putstringset(string key, set<string> values);    //向key键里写入set类型的值
boolean commit();          //提交数据,写入io

 

4.使用3种方法生成sharedpreferences配置文件示例

写mainactivity.java的oncreate():

public class mainactivity extends activity {

    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);

        sharedpreferences sp1 = mainactivity.this.getsharedpreferences("info1", mode_private);
        //写入数据
        editor edit1 = sp1.edit();
        edit1.putstring("name", "test1");
        edit1.commit();                
        //读取数据
        log.v("mainactivity","sp1: "+sp1.getstring("name", ""));

        sharedpreferences sp2 = mainactivity.this.getpreferences(mode_private);
        //写入数据
        editor edit2 = sp2.edit();
        edit2.putstring("name", "test2");
        edit2.commit();
        //读取数据
        log.v("mainactivity","sp2: "+sp1.getstring("name", ""));

        sharedpreferences sp3 = preferencemanager.getdefaultsharedpreferences(mainactivity.this);
        //写入数据
        editor edit3 = sp3.edit();
        edit3.putstring("name", "test3");
        edit3.commit();
        //读取数据
        log.v("mainactivity","sp3: "+sp1.getstring("name", ""));
}

运行后,可以看到在data/data/com.example.sdreadwrite/shared_prefs下自动生成了3个xml文件:

 

  • info1.xml: 通过mainactivity.this.getsharedpreferences("info1", mode_private);实现的
  • mainactivity.xml: 通过mainactivity.this.getpreferences(mode_private);实现的
  • com.example.sdreadwrite_preferences.xml: 通过preferencemanager.getdefaultsharedpreferences(mainactivity.this);实现的

打开info1.xml试试:

 

打开log打印(可以看到读出来的内容和xml配置一样):

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网