一、简介
在开发中,很多重要的瞬时数据都需要保存起来让数据持久化,即 Android 中的数据持久化技术。相信这个对于大家来说并不陌生,常用的如文件存储、数据库存储、SharedPreferences ,以及网络存储等。今天要介绍的便是其中之一的 SharedPreferences 。
SharedPreferences 是一个轻量级的存储类,常用于保存软件配置参数、账号和密码等。SharedPreferences 内部使用 xml 文件将数据以 key...value 的格式保存在 “\data\data\应用程序包名\shared_prefs\” 目录下,在 Android Studio 中打开 Device File Explorer 即可查看。下图是 SharedPreferences 可以保存的数据类型。
接下来是实践时间,分别介绍利用 SharedPreferences 存数据和取数据的方法。
二、用法
(一)SharedPreferences 存数据
- 获取 SharedPreferences 对象。Android 中提供了三种方法获取 SharedPreferences 对象:
- Context 类中的 getSharedPreferences() 方法
/**
* Activity中直接getSharedPreferences,Fragment中需要getActivity().getSharedPreferences
* 参数一:SharedPreferences文件名
* 参数二:操作模式,Android6.0之后仅MODE_PRIVATE一种模式可选,表示只有当前应用程序才能对参数①中的文件进行读写
*/
SharedPreferences sp = getSharedPreferences("user_info", MODE_PRIVATE);
- Activity 类中的 getPreferences() 方法
/**
* Activity中直接getPreferences,Fragment中需要getActivity().getPreferences()
* 参数:操作模式(此种方式获取SharedPreferences对象时会自动使用当前Activity的类名作为文件名,所以只需一个参数)
*/
SharedPreferences sp = getPreferences(MODE_PRIVATE);
- PreferencesManager 类中的 getDefaultSharedPreferences() 方法
/**
* PreferenceManager类的静态方法,自动使用当前应用程序包名作为前缀来命名SharedPreferences文件,操作模式自动为MODE_PRIVATE
* 参数:上下文
*/
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
- 调用 SharedPreferences 对象的 edit() 方法获取 SharedPreferences.Editor 对象:
SharedPreferences.Editor editor = sp.edit();
- 调用 SharedPreferences.Editor 对象的 putXXX() 方法添加需要保存的数据:
Set<String> userInfo = new HashSet<>();
userInfo.add("张三");
userInfo.add("20");
editor.putString("name", "张三");
editor.putBoolean("isAdult", true);
editor.putFloat("height", 178.00F);
editor.putInt("age", 20);
editor.putLong("IDNum", 123456789000L);
editor.putStringSet("user", userInfo);
- 调用 SharedPreferences.Editor 对象的 apply() 方法或者 commit() 方法提交刚刚添加的数据:
//两种提交方式都可以,任选其一即可
editor.apply();
editor.commit();
至此,利用 SharedPreferences 存储数据的方法就介绍完了,下面是完整实例:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences sp = getSharedPreferences("user_info", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
Set<String> userInfo = new HashSet<>();
userInfo.add("张三");
userInfo.add("20");
editor.putString("name", "张三");
editor.putBoolean("isAdult", true);
editor.putFloat("height", 178.00F);
editor.putInt("age", 20);
editor.putLong("IDNum", 123456789000L);
editor.putStringSet("userInfo", userInfo);
editor.apply();
}
}
运行程序以后,数据就已经保存成功了,眼见为实,在 Android Studio 中打开 Device File Explorer ,进入 “\data\data\应用程序包名\shared_prefs\” 目录下,结果如下:
(二)SharedPreferences 取数据
- 获取 SharedPreferences 对象。利用 SharedPreferences 存数据时已经介绍过了获取 SharedPreferences 对象的方法,这里不再赘述,直接看代码:
/**
* 参数一:SharedPreferences文件名,必须和存数据时的文件名一致,存什么就取什么
* 参数二:操作模式
*/
SharedPreferences sp = getSharedPreferences("user_info", MODE_PRIVATE);
- 调用 SharedPreferences 对象的 getXXX() 方法,根据存数据时约定的 key 值获取对应的数据:
/**
* SharedPreferences对象的.getXXX()方法接收两个参数,该方法有返回值,返回key对应的value
* 参数一:存储数据时使用的key
* 参数二:默认值,如果取值失败则返回默认值
*/
String name = sp.getString("name", "");
boolean isAdult = sp.getBoolean("isAdult", false);
float height = sp.getFloat("height", 0F);
int age = sp.getInt("age", 0);
long IDNum = sp.getLong("IDNum", 0L);
HashSet<String> userInfo = (HashSet<String>) sp.getStringSet("userInfo", new HashSet<String>());
至此,利用 SharedPreferences 取出数据的方法就介绍完了,下面是完整实例:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences sp = getSharedPreferences("user_info", MODE_PRIVATE);
String name = sp.getString("name", "");
boolean isAdult = sp.getBoolean("isAdult", false);
float height = sp.getFloat("height", 0F);
int age = sp.getInt("age", 0);
long IDNum = sp.getLong("IDNum", 0L);
HashSet<String> userInfo = (HashSet<String>) sp.getStringSet("userInfo", new HashSet<String>());
Log.d("MyLog", "姓名:" + name);
Log.d("MyLog", "是否成年:" + isAdult);
Log.d("MyLog", "身高:" + height);
Log.d("MyLog", "年龄:" + age);
Log.d("MyLog", "身份证号码:" + IDNum);
Log.d("MyLog", "用户信息:" + userInfo.toString());
}
}
运行程序后,Logcat 打印的日志结果如下:
三、apply 与 commit 的区别
- commit
/**
* Commit your preferences changes back from this Editor to the
* {@link SharedPreferences} object it is editing. This atomically
* performs the requested modifications, replacing whatever is currently
* in the SharedPreferences.
*
* <p>Note that when two editors are modifying preferences at the same
* time, the last one to call commit wins.
*
* <p>If you don't care about the return value and you're
* using this from your application's main thread, consider
* using {@link #apply} instead.
*
* @return Returns true if the new values were successfully written
* to persistent storage.
*/
boolean commit();
如上是 Editor.commit 方法的源码,英文好一点的童鞋能从源码注释中了解不少东西,英语差点的童鞋不要着急,这里我提取出源码注释中的一些关键信息,如下:
- commit 方法用于将需要保存的数据通过 Editor 提交并保存到 SharedPreferences 对象中;
- commit 提交过程属于原子操作,也就是该提交过程不可中断,要么全部提交成功,要么全部提交失败,不会出现只保存了其中一部分数据的情况;
- 同一个 SharedPreferences 对象,多次调用 commit 方法,最后一次的调用会覆盖掉前面提交的数据,也就是当出现多次 commit 时,以最后一次 commit 提交的数据为准;
- commit 方法有返回值( boolean 类型),意思是如果本次写入数据成功,返回 true,否则返回 false 。
- 如果不关心返回值(不关心数据是否写入成功),推荐使用 apply 方法提交数据。
- apply
/**
* Commit your preferences changes back from this Editor to the
* {@link SharedPreferences} object it is editing. This atomically
* performs the requested modifications, replacing whatever is currently
* in the SharedPreferences.
*
* <p>Note that when two editors are modifying preferences at the same
* time, the last one to call apply wins.
*
* <p>Unlike {@link #commit}, which writes its preferences out
* to persistent storage synchronously, {@link #apply}
* commits its changes to the in-memory
* {@link SharedPreferences} immediately but starts an
* asynchronous commit to disk and you won't be notified of
* any failures. If another editor on this
* {@link SharedPreferences} does a regular {@link #commit}
* while a {@link #apply} is still outstanding, the
* {@link #commit} will block until all async commits are
* completed as well as the commit itself.
*
* <p>As {@link SharedPreferences} instances are singletons within
* a process, it's safe to replace any instance of {@link #commit} with
* {@link #apply} if you were already ignoring the return value.
*
* <p>You don't need to worry about Android component
* lifecycles and their interaction with <code>apply()</code>
* writing to disk. The framework makes sure in-flight disk
* writes from <code>apply()</code> complete before switching
* states.
*
* <p class='note'>The SharedPreferences.Editor interface
* isn't expected to be implemented directly. However, if you
* previously did implement it and are now getting errors
* about missing <code>apply()</code>, you can simply call
* {@link #commit} from <code>apply()</code>.
*/
void apply();
如上是 Editor.apply 方法的源码,这里,我照例提取出该方法的源码注释中的关键信息,如下:
- 跟 commit 方法一样,apply 方法用于将需要保存的数据通过 Editor 提交并保存到 SharedPreferences 对象中;
- 跟 commit 方法一样,同一个 SharedPreferences 对象,多次调用 apply 方法,最后一次的调用会覆盖掉前面提交的数据,也就是当出现多次 apply 时,以最后一次 apply 提交的数据为准;