Android数据储存之SharedPreferences

SharedPreferences简介

  • SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置。
  • SharedPreferences提供了java常规的Long、Int、String等类型数据的保存接口。
  • SharedPreferences类似过去Windows系统上的ini配置文件,但是它分为多种权限,可以全局共享访问。
  • 提示最终是以xml方式来保存,整体效率来看不是特别的高,对于常规的轻量级而言比SQLite要好不少,如果真的存储量不大可以考虑自己定义文件格式。

SharedPreferences数据的四种操作模式

  • Context.MODE_PRIVATE
  • Context.MODE_APPEND
  • Context.MODE_WORLD_READABLE
  • Context.MODE_WORLD_WRITEABLE

Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件.
Context.MODE_WORLD_READABLEContext.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件.
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取.
MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入

特别注意:出于安全性的考虑,MODE_WORLD_READABLE 和 MODE_WORLD_WRITEABLE 在Android 4.2版本中已经被弃用

SharedPreference操作实例

目标:创建一个输入框,将输入的内容保存至本地,同时可以将其读出来

创建布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="15dp">

    <EditText
        android:id="@+id/rrrtv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入内容"/>

    <Button
        android:id="@+id/bt_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:text="保存"/>

    <Button
        android:id="@+id/bt_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示"/>

    <TextView
        android:id="@+id/tv_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

通过SharedPreferences将内容input/output出来

package com.example.two.datastorage;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.two.R;

public class ShpfActivity extends AppCompatActivity {

    private EditText editText;
    private Button bt_save,bt_show;
    private TextView tv_show;
    private SharedPreferences spf;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shpf);

        bt_save = findViewById(R.id.bt_save);
        bt_show = findViewById(R.id.bt_show);
        editText = findViewById(R.id.rrrtv);
        tv_show = findViewById(R.id.tv_show);

        spf = getSharedPreferences("data",MODE_PRIVATE);
        editor = spf.edit();

        bt_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editor.putString("name",editText.getText().toString());
                editor.apply();
            }
        });

        bt_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_show.setText(spf.getString("name",""));
            }
        });
    }
}

效果如图所示
image

获取SharedPreferences的三种方式

  • 调用Context对象的getSharedPreferences(String name,int mode)方法

    • name:是指文件名称,不需要加后缀.xml,系统会自动为我们加上

    • mode:是指定读写方式

  • 调用Activity对象的getPreferences(int mode)方法

两种方式的区别:

调用Context对象的getSharedPreferences()方法获得的SharedPreferences对象可以被同一应用程序下的其他组件共享
调用Activity对象的getPreferences()方法获得的SharedPreferences对象只能在该Activity中使用

  • getDefaultSharedPreferences(): 每个应用有一个默认的偏好文件preferences.xml,使用该方法获取
posted @ 2022-01-08 20:44  又一岁荣枯  阅读(74)  评论(0编辑  收藏  举报