推荐阅读:
Android 文件读写最全解析
Android 通过HttpURLConnection访问Http协议网络
Android SQlite数据库使用详解
Android xUtils框架最全使用详解
Android 动态权限最全解析
SharedPreferences概述
SharedPreferences
是Android平台上一个轻量级的存储辅助类,用来保存应用的一些常用配置,它提供了String
,set
,int
,long
,float
,boolean
六种数据类型。SharedPreferences
的数据以键值对的进行保存在以xml
形式的文件中。在应用中通常做一些简单数据的持久化缓存。
SharedPreferences使用
增加数据
//获取SharedPreferences对象
SharedPreferences sharedPreferences = getSharedPreferences("user",MODE_PRIVATE);
//获取Editor对象的引用
SharedPreferences.Editor editor = sharedPreferences.edit();
//将获取过来的值放入文件
editor.putString("name", "lucas");
editor.putInt("age", 30);
editor.putBoolean("islogin",true);
// 提交数据
editor.commit();
editor类核心方法
从editor的put方法可以看出SharedPreferences提供了string,set,int,long,float,boolean六种数据类型。
getSharedPreferences
方法参数详解
@Override
public SharedPreferences getSharedPreferences(String name, int mode) {
}
- name 存储文件名字
- mode 存储方式 其值的含义如下
Context.MODE_PRIVATE | 指定该SharedPreferences数据只能被本应用程序读、写 |
---|---|
Context.MODE_WORLD_READABLE | 指定该SharedPreferences数据能被其他应用程序读,但不能写 |
Context.MODE_WORLD_WRITEABLE | 指定该SharedPreferences数据能被其他应用程序读 |
Context.MODE_APPEND | 该模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件; |
点击添加按钮以后我们可以看到在data/data/应用程序包名
的shared_prefs
文件夹下生成了一个user.xml
的xml文件。点击可以打开该文件,可以看到该文件保存了如下的数据
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="name">lucas</string>
<int name="age" value="30" />
<boolean name="islogin" value="true" />
</map>
读取数据
读取数据可以通过获取SharedPreferences
对象,通过SharedPreferences
对象可以获取存储的数据值,第二个参数一般是个默认值,表示当获取数据的时候没有该key则返回一个默认值。
SharedPreferences sharedPreferences= getSharedPreferences("user", MODE_PRIVATE);
String name=sharedPreferences.getString("name","");
int age = sharedPreferences.getInt("age",0);
boolean islogin = sharedPreferences.getBoolean("islogin",true);
Log.i("lucashu","name:"+ name +" age:" + age +" islogin:" + islogin);
删除数据
删除数据跟添加数据有点类似 也是通过Editor
对象来完成。
//获取SharedPreferences对象
SharedPreferences sharedPreferences = getSharedPreferences("user",MODE_PRIVATE);
//获取Editor对象的引用
SharedPreferences.Editor editor = sharedPreferences.edit();
//将获取过来的值放入文件
editor.remove("name");
// 提交数据
editor.commit();
修改数据
修改数据跟增加数据类似,覆盖原来的数据即修改数据。
//获取SharedPreferences对象
SharedPreferences sharedPreferences = getSharedPreferences("user",MODE_PRIVATE);
//获取Editor对象的引用
SharedPreferences.Editor editor = sharedPreferences.edit();
//将获取过来的值放入文件
editor.putString("name", "lucas1");
editor.putInt("age", 31);
editor.putBoolean("islogin",false);
// 提交数据
editor.commit();
清除数据
我们可以通过Editor
对象的clear方法来完成清楚数据
//获取SharedPreferences对象
SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
//获取Editor对象的引用
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
// 提交数据
editor.commit();
清除以后user.xml
文件并不会删除,但里面的数据都没了
Demo代码
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = findViewById(R.id.button2); //add
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取SharedPreferences对象
SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
//获取Editor对象的引用
SharedPreferences.Editor editor = sharedPreferences.edit();
//将获取过来的值放入文件
editor.putString("name", "lucas");
editor.putInt("age", 30);
editor.putBoolean("islogin", true);
// 提交数据
editor.commit();
}
});
Button button2 = findViewById(R.id.button3); //删
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取SharedPreferences对象
SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
//获取Editor对象的引用
SharedPreferences.Editor editor = sharedPreferences.edit();
//将获取过来的值放入文件
editor.remove("name");
// 提交数据
editor.commit();
}
});
Button button3 = findViewById(R.id.button4); //改
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取SharedPreferences对象
SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
//获取Editor对象的引用
SharedPreferences.Editor editor = sharedPreferences.edit();
//将获取过来的值放入文件
editor.putString("name", "lucas1");
editor.putInt("age", 31);
editor.putBoolean("islogin", false);
// 提交数据
editor.commit();
}
});
Button button4 = findViewById(R.id.button5); //查
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
String name = sharedPreferences.getString("name", "");
int age = sharedPreferences.getInt("age", 0);
boolean islogin = sharedPreferences.getBoolean("islogin", true);
Log.i("lucashu", "name:" + name + " age:" + age + " islogin:" + islogin);
}
});
Button button5 = findViewById(R.id.button6); //清除数据
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取SharedPreferences对象
SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
//获取Editor对象的引用
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
// 提交数据
editor.commit();
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:text="增加数据"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="修改数据"
app:layout_constraintEnd_toEndOf="@+id/button2"
app:layout_constraintStart_toStartOf="@+id/button2"
app:layout_constraintTop_toBottomOf="@+id/button2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="删除数据"
app:layout_constraintEnd_toEndOf="@+id/button4"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/button4"
app:layout_constraintTop_toBottomOf="@+id/button4" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="查询数据"
app:layout_constraintEnd_toEndOf="@+id/button3"
app:layout_constraintStart_toStartOf="@+id/button3"
app:layout_constraintTop_toBottomOf="@+id/button3" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="清除数据"
app:layout_constraintEnd_toEndOf="@+id/button5"
app:layout_constraintStart_toStartOf="@+id/button5"
app:layout_constraintTop_toBottomOf="@+id/button5" />
</androidx.constraintlayout.widget.ConstraintLayout>
SharedPreferences封装类
由于SharedPreferences需要操作的代码重复性比较多,我们可以把它封装成一个工具类,这样使用起来会更加方便。
package com.lucashu.android;
import android.content.Context;
import android.content.SharedPreferences;
public class PreferencesUtil {
public static String PREFERENCE_NAME = "user";
/**用户名的key值*/
public static String USERNAME = "username";
/**存储字符串*/
public static boolean putString(Context context, String key, String value) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
return editor.commit();
}
/**读取字符串*/
public static String getString(Context context, String key) {
return getString(context, key, null);
}
/**读取字符串(带默认值的)*/
public static String getString(Context context, String key, String defaultValue) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
return preferences.getString(key, defaultValue);
}
/**存储整型数字*/
public static boolean putInt(Context context, String key, int value) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(key, value);
return editor.commit();
}
/**读取整型数字*/
public static int getInt(Context context, String key) {
return getInt(context, key, -1);
}
/**读取整型数字(带默认值的)*/
public static int getInt(Context context, String key, int defaultValue) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
return preferences.getInt(key, defaultValue);
}
/**存储长整型数字*/
public static boolean putLong(Context context, String key, long value) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putLong(key, value);
return editor.commit();
}
/**读取长整型数字*/
public static long getLong(Context context, String key) {
return getLong(context, key, 0xffffffff);
}
/**读取长整型数字(带默认值的)*/
public static long getLong(Context context, String key, long defaultValue) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
return preferences.getLong(key, defaultValue);
}
/**存储Float数字*/
public static boolean putFloat(Context context, String key, float value) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putFloat(key, value);
return editor.commit();
}
/**读取Float数字*/
public static float getFloat(Context context, String key) {
return getFloat(context, key, -1.0f);
}
/**读取Float数字(带默认值的)*/
public static float getFloat(Context context, String key, float defaultValue) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
return preferences.getFloat(key, defaultValue);
}
/**存储boolean类型数据*/
public static boolean putBoolean(Context context, String key, boolean value) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
return editor.commit();
}
/**读取boolean类型数据*/
public static boolean getBoolean(Context context, String key) {
return getBoolean(context, key, false);
}
/**读取boolean类型数据(带默认值的)*/
public static boolean getBoolean(Context context, String key, boolean defaultValue) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
return preferences.getBoolean(key, defaultValue);
}
/**清除数据*/
public static boolean clearPreferences(Context context) {
SharedPreferences pref = context.getSharedPreferences(PREFERENCE_NAME, 0);
SharedPreferences.Editor editor = pref.edit();
editor.clear();
return editor.commit();
}
}
作者:lucashu
出处:https://blog.csdn.net/huweiliyi/article/details/105496932
原创不易,欢迎转载,但未经作者同意请保留此段声明,并在文章页面明显位置给出原文链接。