(一)Android数据存储————文件存储(内部存储):https://blog.csdn.net/sinat_29675423/article/details/85699131
(二)Android数据存储————文件存储(外部存储):https://blog.csdn.net/sinat_29675423/article/details/85705545
本文主要介绍sharedpreference的基本特征和使用方式,然后将其数据存取方式封装到一个工具类SharedPreferenceUtil中,便于调用。
实例:首次启动欢迎页
存储特点
sharedPreferences是一种轻型的数据存储方式,它的本质是基于xml文件存储key-value键值对数据,通常用来存储一些简单的配置信息。
数据存储方式
存储位置
其存储位置在/data/data/<包名>/shared_prefs目录下。
使用方法
获取sharedpreference对象:
Android获取SharedPreferences三种方式:
- context.getPreferences(@PreferencesMode int mode):获取的是本Activity私有的Preference,保存在系统中的xml形式的文件的名称为这个Activity的名字,因此一个Activity只能有一个,属于这个Activity。
- context.getSharedpreferences(String name, @PreferencesMode int mode):通过Activity对象获取,属于整个应用程序,以第一参数的name为文件名保存在系统中,可以有多个。
- PreferenceManager.getDefaultSharedPreferences(Context context):PreferenceManager的静态函数,保存PreferenceActivity中的设置,属于整个应用程序,但是只有一个,Android会根据包名和PreferenceActivity的布局文件来起一个名字保存。
mode
- Activity.MODE_PRIVATE 默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
- Activity.MODE_APPEND 把新写入的内容追加到原文件中
读取数据
sharedPreferences.getString(key,(String) defValue)后面的value为默认值
注:sharedPreferences对象本身只能获取数据而不支持存储和修改,存储和修改是通过Editor对象实现的。
获取Editor对象
sharedPreferences.edit()
将键值对存入editor中
Editor.putString(key,value)
Editor.putInt(key,value)
可以支持的数据类型有:
提交
Editor.commit()同步进行
editor.apply()异步进行
封装SharedPreferenceUtil工具类
存入数据:SharedPreferenceUtil.getInstance().put(Context context, String key, Object value)
读取数据:SharedPreferenceUtil.getInstance().get(Context context, String key, Object defValue)
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;
/**
* @function sharedPreference 工具类
*/
public final class SharedPreferenceUtil {
private static final String FILE_NAME = "hello"; //文件名
private static SharedPreferenceUtil mInstance;
private SharedPreferenceUtil(){}
public static SharedPreferenceUtil getInstance(){
if(mInstance == null){
synchronized (SharedPreferenceUtil.class){
if(mInstance == null){
mInstance = new SharedPreferenceUtil();
}
}
}
return mInstance;
}
/**
* 存入键值对
* @param context
* @param key
* @param value
*/
public void put(Context context, String key, Object value){
//判断类型
String type = value.getClass().getSimpleName();
SharedPreferences sharedPreferences = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
if("Integer".equals(type)){
editor.putInt(key,(Integer)value);
}else if ("Boolean".equals(type)){
editor.putBoolean(key,(Boolean)value);
}else if ("Float".equals(type)){
editor.putFloat(key,(Float)value);
}else if ("Long".equals(type)){
editor.putLong(key,(Long)value);
}else if ("String".equals(type)){
editor.putString(key,(String) value);
}
editor.apply();
}
/**
* 读取键的值,若无则返回默认值
* @param context
* @param key
* @param defValue 默认值
* @return
*/
@Nullable
public Object get(Context context, String key, Object defValue){
SharedPreferences sharedPreferences = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
String type = defValue.getClass().getSimpleName();
if("Integer".equals(type)){
return sharedPreferences.getInt(key,(Integer)defValue);
}else if ("Boolean".equals(type)){
return sharedPreferences.getBoolean(key,(Boolean)defValue);
}else if ("Float".equals(type)){
return sharedPreferences.getFloat(key,(Float)defValue);
}else if ("Long".equals(type)){
return sharedPreferences.getLong(key,(Long)defValue);
}else if ("String".equals(type)){
return sharedPreferences.getString(key,(String) defValue);
}
return null;
}
}
实例: APP首次启动欢迎页
首次启动时,app展示欢迎页,非首次启动时,app进入首页。逻辑简单,不赘述,直接贴代码。
WelcomeActivity
public class WelcomeActivity extends AppCompatActivity {
private Button btEnter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
btEnter = findViewById(R.id.btEnter);
boolean isFirst = checkIsFirstLunch();
if(isFirst){
btEnter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferenceUtil.getInstance().put(WelcomeActivity.this, "IsFirst", false);
EnterHome();
}
});
}else {
EnterHome();
}
}
private void EnterHome() {
startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
finish();
}
private boolean checkIsFirstLunch() {
boolean isFirst = (boolean) SharedPreferenceUtil.getInstance()
.get(WelcomeActivity.this, "IsFirst", true);
return isFirst;
}
}
MainActivity >>>
任何一个你的主页…helloworld也可以…
布局文件 welcome
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".WelcomeActivity">
<Button
android:id="@+id/btEnter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="进入首页"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/tvWelcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是欢迎页"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>