SharePreference是五种数据保存的方式的其中一种,其他四种是内部数据存储,外部数据存储,数据库存储,序列化存储。
这是一个很方便SharePreference数据保存和获取的工具类。这里的数据也是保存到本地的。程序关闭后,下次打开程序还是还可以读取到保存的数据。
一.工具类的设计
package com.android.utils;
import android.content.Context;
import android.content.SharedPreferences;
/**
* 这是一个SharePreference的工具类,使用它可以更方便的数据进行简单存储和提取
* 这里只要知道基本调用方法就可以了
* 1.通过构造方法来传入上下文和文件名
* 2.通过putValue方法传入一个或多个自定义的ContentValue对象,进行数据存储
* 3.通过getXXX方法来获取数据
* 4.通过clear方法来清除这个文件的数据
* 这里没有提供清除单个key的数据,是因为存入相同的数据会自动覆盖,没有必要去理会,当然也是可以添加的
*/
public class SharePreferencesHelper {
//定义一个SharePreference对象
SharedPreferences sharedPreferences;
//定义一个上下文对象
//创建SharePreference对象时要上下文和存储的模式
//通过构造方法传入一个上下文
public SharePreferencesHelper(Context context, String fileName) {
//实例化SharePreference对象,使用的是get方法,而不是new创建
//第一个参数是文件的名字
//第二个参数是存储的模式,一般都是使用私有方式:Context.MODE_PRIVATE
sharedPreferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
}
/**
* 存储数据
* 这里要对存储的数据进行判断在存储
* 只能存储简单的几种数据
* 这里使用的是自定义的ContentValue类,来进行对多个数据的处理
*/
//创建一个内部类使用,里面有key和value这两个值
public static class ContentValue {
String key;
Object value;
//通过构造方法来传入key和value
public ContentValue(String key, Object value) {
this.key = key;
this.value = value;
}
}
//一次可以传入多个ContentValue对象的值
public void putValues(ContentValue... contentValues) {
//获取SharePreference对象的编辑对象,才能进行数据的存储
SharedPreferences.Editor editor = sharedPreferences.edit();
//数据分类和存储
for (ContentValue contentValue : contentValues) {
//如果是字符型类型
if (contentValue.value instanceof String) {
editor.putString(contentValue.key, contentValue.value.toString()).commit();
}
//如果是int类型
if (contentValue.value instanceof Integer) {
editor.putInt(contentValue.key, Integer.parseInt(contentValue.value.toString())).commit();
}
//如果是Long类型
if (contentValue.value instanceof Long) {
editor.putLong(contentValue.key, Long.parseLong(contentValue.value.toString())).commit();
}
//如果是布尔类型
if (contentValue.value instanceof Boolean) {
editor.putBoolean(contentValue.key, Boolean.parseBoolean(contentValue.value.toString())).commit();
}
}
}
//获取数据的方法
public String getString(String key) {
return sharedPreferences.getString(key, null);
}
public boolean getBoolean(String key) {
return sharedPreferences.getBoolean(key, false);
}
public int getInt(String key) {
return sharedPreferences.getInt(key, -1);
}
public long getLong(String key) {
return sharedPreferences.getLong(key, -1);
}
//清除当前文件的所有的数据
public void clear() {
sharedPreferences.edit().clear().commit();
}
}
二.调用的示例
package com.example.utils;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.android.utils.SharePreferencesHelper;
import com.android.utils.SharePreferencesHelper.ContentValue;
/**
*sharePreference的使用演示
*
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
//通过构造方法来传入上下文和文件名
SharePreferencesHelper helper=new SharePreferencesHelper(this,"myInfo");
//下面可以做增删改查操作了
if ("liwen".equals(helper.getString("name"))) {//判断某个用户名是否存在
Log.e("TAG","用户名存在!");
}else {//如果不存在就输入一些数据
Log.e("TAG","用户名不存在,添加數據!");
//注意这里的ContentValue是自定义的内部类,不是系统的类
ContentValue contentValues1=new ContentValue("name","liwen");
ContentValue contentValues2=new ContentValue("sex","男");
ContentValue contentValues3=new ContentValue("age",18);
//存入三个值
helper.putValues(contentValues1,contentValues2,contentValues3);
}
//取出这些值
String name=helper.getString("name");
String sex=helper.getString("sex");
int age=helper.getInt("age");
//如果要修改数据,直接put覆盖就可以了
//如果要清除数据就用,helper.clear();
//显示效果
Log.e("TAG","name="+name+",sex="+sex+",age="+age);
}
}
三.调用之后显示的Log信息
如果直接获取不存在的数据,不会报错会获取到空值,如果是获取数字不存在就是获取到0
这里使用SharePreference存储数据,只能存储一些基础的数据,
不能存储很复杂的对象,一般的作用是用于用户的自动登陆,一些开关的设置,
比如查看用户是否设置的是夜间模式等等。