版权声明:转载请标明出处 https://blog.csdn.net/easy_purple/article/details/80569743
(此博文使用的工具为Eclipse)
首先介绍一下SharedPreferences是使用键值对的方式来存储数据的,每次存储数据的时候需要给这条数据提供一个对应的键,这样读取数据的时候就可以根据这个键吧对应的值取出来。
使用SharedPreferences来进行数据持久化要比使用文件方便很多。
1、将数据存储到SharedPreferences中:
要想使用SharedPreferences存储数据,首先要获取到SharedPreferences对象,Android中提供了三种方法用于
获得SharedPreferences对象:
-
Context类中的getSharedPreferences()方法
此方法接受两个参数,第一个用于指定SharedPreferences文件的名称,如果指定的文件不存在则会创建一个,SharedPreferences文件都是存放在/data/data/<package name>/shared-prefs/目录下的;第二个参数用于指定操作模式MODE_PRIVATE,表示只有当前的应用程序才可以对这个SharedPreferences文件进行读写。
-
Activity中的getSharedPreferences方法
这个方法和上一个方法类似,不过它只接受一个操作模式参数,因为使用这个方法是会自动将当前活动的类名作为SharedPreferences的文件名
-
PreferenceManager类中的getDefaultSharedPreferences()方法
这是一个静态方法,,它接受一个Context参数,并自动使用当前应用程序的包名作为前缀来命名SharedPreferences文件。
得到了SharedPreferences对象后,就可以开始向SharedPreferences文件中存储数据了,主要分三步:
-
调用SharedPreferences对象的edit()方法来获取一个SharedPreferences。Editor对象
-
向SharedPreferences.Editor对象中添加数据
-
调用apply()方法将添加的数据进行提交,从而完成数据存储操作。
(提交之后,借助File Explorer来进行查看,打开Android Device Monitor,并点击File Explorer标签页,然后进入/data/data/<package name>/shared-prefs/目录下,就会看到一个data.xml文件)
代码如:
SharedPreferences.Editor editor = getSharedPreferences("data",
MODE_PRIVATE).edit();
editor.putString("name", "tom");
editor.putInt("age", 66);
editor.putBoolean("isMarried", false);
editor.apply();
2、从SharedPreferences读取数据
SharedPreferences对象中提供了一系列的get方法用于对存储的数据进行读取,每种方法都对应了SharedPreferences.Editor中的一种put方法,这些get方法都会接受两个参数,第一个参数是键,第二个是默认值,即表示当传入的键找不到对应的值是会以此默认值作为返回数据。
代码如:
SharedPreferences pref=getSharedPreferences("data", MODE_PRIVATE);
String name=pref.getString("name", "");
int age=pref.getInt("age", 0);
Boolean isMarried=pref.getBoolean("isMarried", false);
运行结果展示:
点击写入按钮后打印日志如下:
点击读取按钮后打印日志如下:
所有的代码:
MainActivity
package com.example.practice_preferences;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
Button write, read;
private final String TAG="read&write";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();// 绑定按钮
initEvent();// 绑定事件
}
private void initEvent() {
write.setOnClickListener(this);
read.setOnClickListener(this);
}
private void initView() {
write = (Button) findViewById(R.id.id_write);
read = (Button) findViewById(R.id.id_read);
}
@SuppressLint("NewApi") public void onClick(View v) {
switch (v.getId()) {
case R.id.id_write:
SharedPreferences.Editor editor = getSharedPreferences("data",
MODE_PRIVATE).edit();
editor.putString("name", "tom");
editor.putInt("age", 66);
editor.putBoolean("isMarried", false);
editor.apply();
Log.i(TAG, "写入成功");
break;
case R.id.id_read:
SharedPreferences pref=getSharedPreferences("data", MODE_PRIVATE);
String name=pref.getString("name", "");
int age=pref.getInt("age", 0);
Boolean isMarried=pref.getBoolean("isMarried", false);
Log.i(TAG, "读取成功...");
Log.i(TAG, "name is : "+name);
Log.i(TAG, "age is : "+age);
Log.i(TAG, "isMarried?"+isMarried);
break;
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/id_write"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写入" />
<Button
android:id="@+id/id_read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取" />
</LinearLayout>
最后说一下,SharedPreferences的用途:记录此应用打开了多少次、实现记住密码功能等等。。。当然不止我说的,毕竟这是一个工具,我们要学会举一反三。