您的位置:首页 > 移动开发 > Android开发

文章标题

2015-07-06 20:06 639 查看

android共享存储SharedPreferences

适用范围

保存少量的数据,且这些数据的格式非常简单:字符串型、基本类型的值。比如应用程序的各种配置信息(如是否打开音效、是否使用震动效果、小游戏的玩家积分等),解锁口 令密码等

核心原理

保存基于XML文件存储的key-value键值对数据,通常用来存储一些简单的配置信息。

SharedPreferences数据总是存储在/data/data//shared_prefs目录下。

SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过SharedPreferences.edit()获取的内部接口Editor对象实现。

SharedPreferences本身是一个接口,程序无法直接创建SharedPreferences实例,只能通过Context提供的getSharedPreferences(String name, int mode)方法来获取SharedPreferences实例

该方法中name表示要操作的xml文件名,第二个参数具体如下:

Context.MODE_PRIVATE: 指定该SharedPreferences数据只能被本应用程序读、写。

Context.MODE_WORLD_READABLE: 指定该SharedPreferences数据能被其他应用程序读,但不能写。

Context.MODE_WORLD_WRITEABLE: 指定该SharedPreferences数据能被其他应用程序读,写

Editor有如下主要重要方法:

SharedPreferences.Editor clear():清空SharedPreferences里所有数据

SharedPreferences.Editor putXxx(String key , xxx value): 向SharedPreferences存入指定key对应的数据,其中xxx 可以是boolean,float,int等各种基本类型据

SharedPreferences.Editor remove(): 删除SharedPreferences中指定key对应的数据项

boolean commit(): 当Editor编辑完成后,使用该方法提交修改

创建共享存储文件

主要使用的是SharedPreferences接口和SharedPreferences.Editor接口

读取共享存储文件的内容

// 读取共享文件里面的数据
// 使用上下文对象的getSharedPreferences获取S
4000
haredPreferences对象
// 参数一:代表文件名
// 参数二:共享文件的模式,Context.MODE_PRIVATE代表本项目可用
sharedPreferences = this.getSharedPreferences("myfile", Context.MODE_PRIVATE);
// 使用sharedPreferences对象的getxxx方法获取键值信息
String str1 = sharedPreferences.getString("textView1", "");
String str2 = sharedPreferences.getString("textView2", "");
String str3 = sharedPreferences.getString("textView3", "");
textView1.setText(str1);
textView2.setText(str2);
textView3.setText(str3);


设置共享存储文件的内容

// 写入数据使用的是SharedPreferences.Editor对象,可以使用sharedPreferences.edit()方法获得
Editor editor = sharedPreferences.edit();
editor.putString("textView1", "111111");
editor.putString("textView2", "222222");
editor.putString("textView3", "333333");
//需要提交
editor.commit();


完整的代码

package com.hzj.act;

import com.hzj.myshareddb.R;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainAct extends Activity {

TextView textView1;
TextView textView2;
TextView textView3;

SharedPreferences sharedPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_act);
textView1 = (TextView) this.findViewById(R.id.textView1);
textView2 = (TextView) this.findViewById(R.id.textView2);
textView3 = (TextView) this.findViewById(R.id.textView3);

// 读取共享文件里面的数据
// 使用上下文对象的getSharedPreferences获取SharedPreferences对象
// 参数一:代表文件名
// 参数二:共享文件的模式,Context.MODE_PRIVATE代表本项目可用
sharedPreferences = this.getSharedPreferences("myfile", Context.MODE_PRIVATE);
// 使用sharedPreferences对象的getxxx方法获取键值信息
String str1 = sharedPreferences.getString("textView1", "");
String str2 = sharedPreferences.getString("textView2", "");
String str3 = sharedPreferences.getString("textView3", "");
textView1.setText(str1);
textView2.setText(str2);
textView3.setText(str3);
}

public void btn1(View v) {
// 写入数据使用的是SharedPreferences.Editor对象,可以使用sharedPreferences.edit()方法获得
Editor editor = sharedPreferences.edit();
editor.putString("textView1", "111111");
editor.putString("textView2", "222222");
editor.putString("textView3", "333333");
// 需要提交
editor.commit();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息