Android持久化存储之SharedPreferences
2020-02-28 本文已影响0人
GODANDDEVIL
SharedPreferences是一种轻量型的存储方式,他适合用于存储一些简单的参数配置。比如说在游戏中是否打开声音、上一次登录的账号等,这些都可以用SharedPreferences来实现。其实SharedPreferences的原理是使用xml文件来存放数据。比如说我们在res/values下新建一个setting.xml文件。文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="number"></string>
<string name="music"></string>
</resources>
具体使用步骤:
public class LoginActivity extends AppCompatActivity
{
//声明SharedPreferences,用来读取xml
SharedPreferences spf;
//声明SharedPreferences.Editor,用来修改xml里面的值
SharedPreferences.Editor edit;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//与相应的xml文件建立连接
spf = getSharedPreferences("setting",MODE_PRIVATE);
//直接使用getxxxx方法来获取xml中指定key的值,第二个参数是当不存在这个数据时,会返回的值
String musicValue = spf.getString("music","");
//得到指定xml的SharedPreferences.Editor
edit = spf.edit();
//修改指定xml的值
ed.putString("music","xxx");
//记住,这里一定要commit才能生效。
ed.commit();
}
}
在打开指定xml时,调用了getSharedPreferences(“setting”,MODE_WORLD_READABLE)这个方法,其中第一个就是当前要打开xml的名字。而第二个参数就是以什么样的方式打开xml,一共有四种方式:
- MODE_APPEND: 追加方式存储
- MODE_PRIVATE: 私有方式存储,其他应用无法访问
- MODE_WORLD_READABLE: 表示当前文件可以被其他应用读取
- MODE_WORLD_WRITEABLE: 表示当前文件可以被其他应用写入
如果采用3与4两种方式创建或打开的话。这个Preferences将可以被其他应用打开,打开的时候其他应用需要得到当前应用的Context。然后通过Context.getSharedPreferences()打开。
SharePreference单例:
import android.content.Context;
import android.content.SharedPreferences;
/**
* SharePreference工具类
*/
public class SharePreferenceUtils {
//文件名
private static final String FILE_NAME = "setting";
//key
public static final String DEVICE_NAME = "device_name";
public static final String IP = "ip";
public static final String PORT = "port";
/**
* 保存数据
*/
public static void put(Context context, String key, Object object) {
SharedPreferences.Editor editor = getEditor(context);
if (object instanceof String) {
editor.putString(key, (String) object);
} else if (object instanceof Integer) {
editor.putInt(key, (Integer) object);
} else if (object instanceof Boolean) {
editor.putBoolean(key, (Boolean) object);
} else if (object instanceof Float) {
editor.putFloat(key, (Float) object);
} else if (object instanceof Long) {
editor.putLong(key, (Long) object);
} else {
editor.putString(key, object.toString());
}
editor.commit();
}
/**
* 获取数据
*/
public static Object get(Context context, String key, Object defaultValue) {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
if (defaultValue instanceof String) {
return sp.getString(key, (String) defaultValue);
} else if (defaultValue instanceof Integer) {
return sp.getInt(key, (Integer) defaultValue);
} else if (defaultValue instanceof Boolean) {
return sp.getBoolean(key, (Boolean) defaultValue);
} else if (defaultValue instanceof Float) {
return sp.getFloat(key, (Float) defaultValue);
} else if (defaultValue instanceof Long) {
return sp.getLong(key, (Long) defaultValue);
}
return null;
}
/**
* remove key
*/
public static void remove(Context context,String key){
SharedPreferences.Editor editor = getEditor(context);
editor.remove(key);
editor.commit();
}
/**
* 判断是否包含key
*/
public static boolean contains(Context context,String key){
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
return sp.contains(key);
}
/**
* 清空数据
*/
public static void clear(Context context){
SharedPreferences.Editor editor = getEditor(context);
editor.clear();
editor.commit();
}
/**
*获取SharedPreferences.Editor
*/
private static SharedPreferences.Editor getEditor(Context context){
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
return sp.edit();
}
}