【Android】6.0SharedPreferences简单数据存储

简介: 如果您有想要保存的相对较小键值对集合,则应使用 SharedPreferences API。SharedPreferences 对象指向包含键值对的文件,并提供读写这些键值对的简单方法。每个 SharedPreferences 文件均由框架进行管理,可以是私有文件,也可以是共享文件。例如:我们登录qq并不是每次都会要输入账号密码,而是会记住密码和用户的登录状态,这些轻量的数据可以通过SharedPreferences以键值对的方式存储在本地


注意:不是教程只是笔记,如有错误欢迎批评指正

官网简介

保存键值对数据  | Android 开发者  | Android Developers (google.cn)

如果您有想要保存的相对较小键值对集合,则应使用 SharedPreferences API。SharedPreferences 对象指向包含键值对的文件,并提供读写这些键值对的简单方法。每个 SharedPreferences 文件均由框架进行管理,可以是私有文件,也可以是共享文件。

例如:我们登录qq并不是每次都会要输入账号密码,而是会记住密码和用户的登录状态,这些轻量的数据可以通过SharedPreferences以键值对的方式存储在本地

🌾SharedPreferences的使用(模拟QQ登录)

这里我们创建三个Activity,分别代表QQ欢迎界面,用户登录界面和用户首页界面

网络异常,图片无法展示
|

😁简单的为登录界面加一些组件

网络异常,图片无法展示
|

activtiy_login.xml关键代码

<?xmlversion="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayoutxmlns: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=".LoginActivity">

 

   <TextView

       android:id="@+id/textView"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_marginStart="52dp"

       android:layout_marginTop="160dp"

       android:text="账号:"

       android:textSize="20sp"

       android:textColor="@color/black"

       app:layout_constraintStart_toStartOf="parent"

       app:layout_constraintTop_toTopOf="parent"/>

 

   <TextView

       android:id="@+id/textView2"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_marginTop="48dp"

       android:text="密码:"

       android:textSize="20sp"

       android:textColor="@color/black"

       app:layout_constraintStart_toStartOf="@+id/textView"

       app:layout_constraintTop_toBottomOf="@+id/textView"/>

 

   <EditText

       android:id="@+id/editTextTextPersonName"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_marginStart="32dp"

       android:ems="10"

       android:inputType="textPersonName"

       android:text="Name"

       app:layout_constraintBottom_toBottomOf="@+id/textView"

       app:layout_constraintStart_toEndOf="@+id/textView"

       app:layout_constraintTop_toTopOf="@+id/textView"/>

 

   <EditText

       android:id="@+id/editTextTextPassword"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:ems="10"

       android:inputType="textPassword"

       app:layout_constraintBottom_toBottomOf="@+id/textView2"

       app:layout_constraintStart_toStartOf="@+id/editTextTextPersonName"

       app:layout_constraintTop_toTopOf="@+id/textView2"/>

 

   <Button

       android:id="@+id/button"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_marginTop="92dp"

       android:text="登录"

       android:textSize="20sp"

       app:layout_constraintEnd_toEndOf="parent"

       app:layout_constraintHorizontal_bias="0.498"

       app:layout_constraintStart_toStartOf="parent"

       app:layout_constraintTop_toBottomOf="@+id/editTextTextPassword"/>

</androidx.constraintlayout.widget.ConstraintLayout>

😆在LoginActivity中写验证登录的逻辑代码

用户如果验证登录成功,我们要将用户的用户名以及登录状态通过SharedPreferences以键值对的形式保存在本地磁盘中,关键代码如下:

//SharedPreferences是接口,并不是类

/*getSharedPreferences(参数一,参数二)

参数一: 存储数据的文件名

参数二: 数据存储方式

*/

//获取SharedPreferences对象

SharedPreferencessp=getSharedPreferences("user",MODE_PRIVATE);

//获取Editor对象的引用

SharedPreferences.Editoreditor=sp.edit();

//将获取过来的值放入文件

editor.putString("username","Starry陆离");

editor.putBoolean("isLogin",true);

//最后要提交数据到本地

editor.commit();

可以看到getSharedPreferences();方法存储数据的方式有多种,其中3种已经在Android7.0中弃用

MODE_PRIVATE 指定该SharedPreferences数据只能被本应用程序
MODE_APPEND 该模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件;
MODE_WORLD_READABLE 指定该SharedPreferences数据能被其他应用程序读,但不能写(不安全,已弃用)
MODE_WORLD_WRITEABLE 指定该SharedPreferences数据能被其他应用程序读取和写入(不安全,已弃用)

网络异常,图片无法展示
|

注意:自 API 级别 17 起,MODE_WORLD_READABLEMODE_WORLD_WRITEABLE 模式已被弃用。 从 Android 7.0(API 级别 24)开始,如果您使用这些模式,Android 会抛出 SecurityException。如果您的应用需要与其他应用共享私有文件,可以通过 FLAG_GRANT_READ_URI_PERMISSION 使用 FileProvider。如需了解详情,另请参阅共享文件

除了可以储存字符串和布尔类型的数据,储存boolean,string,float,int,long,set六种数据类型。

它也有两个参数,前者是”键“,或者是“值”

网络异常,图片无法展示
|

网络异常,图片无法展示
|

LoginActivity完整代码

 

publicclassLoginActivityextendsAppCompatActivity {

 

   @Override

   protectedvoidonCreate(BundlesavedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_login);

 

       EditTextuserNameEdit=findViewById(R.id.editTextTextPersonName);

       EditTextuserPwdEdit=findViewById(R.id.editTextTextPassword);

 

       ButtonloginBtn=findViewById(R.id.button);

 

       loginBtn.setOnClickListener(newView.OnClickListener() {

           @Override

           publicvoidonClick(Viewview) {

               StringuserName=userNameEdit.getText().toString();

               StringuserPwd=userPwdEdit.getText().toString();

               if(userName.equals("starry")&&userPwd.equals("lx0411")){

                   //todo 跳转到主页面+保存用户名+登录状态

 

                   //SharedPreferences是接口,并不是类

                   /*

                   参数一:存储数据的文件名

                    */

                   //获取SharedPreferences对象

 

                   SharedPreferencessp=getSharedPreferences("user",MODE_PRIVATE);

                   //获取Editor对象的引用

                   SharedPreferences.Editoreditor=sp.edit();

                   //将获取过来的值放入文件

                   editor.putString("username",userName);

                   editor.putBoolean("isLogin",true);

                   

                   //最后要提交数据到本地

                   editor.commit();

 

                   //登录成功,跳转到用户首页界面

                   Intentintent=newIntent(LoginActivity.this,MainActivity.class);

                   startActivity(intent);

                   Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_LONG).show();

 

               }else{

                   Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_LONG).show();

               }

           }

       });

 

   }

}

😀用户首页界面添加文本组件

然后简单的为用户首页界面添加一个文本组件用来接受等会储存在SharedPreferences中的用户名

网络异常,图片无法展示
|

activity_main.xml中的完整代码

<?xmlversion="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayoutxmlns: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">

 

   <TextView

       android:id="@+id/textview"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:text="Hello World!"

       android:textColor="@color/black"

       android:textSize="40sp"

       app:layout_constraintBottom_toBottomOf="parent"

       app:layout_constraintLeft_toLeftOf="parent"

       app:layout_constraintRight_toRightOf="parent"

       app:layout_constraintTop_toTopOf="parent"/>

 

</androidx.constraintlayout.widget.ConstraintLayout>

😊在MainActivity中获取到user文件

网络异常,图片无法展示
|

MainActivity完整代码

 

publicclassMainActivityextendsAppCompatActivity {

 

   @Override

   protectedvoidonCreate(BundlesavedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

 

       TextViewtextView=findViewById(R.id.textView);

       //通过文件名获取到user文件

       SharedPreferencessp=getSharedPreferences("user",MODE_PRIVATE);

       //取出键名为username的用户名

       StringuserName=sp.getString("username","");

       //将用户名设置到文本组件中

       textView.setText(userName);

 

   }

}

🤣为用户欢迎界面设置背景

图片可以在在网络上下载qq欢迎界面 - Bing images

网络异常,图片无法展示
|

activity_welcome.xml中的完整代码

<?xmlversion="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayoutxmlns: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"

   android:background="@drawable/welcome"

   tools:context=".WelcomeActivity">

 

</androidx.constraintlayout.widget.ConstraintLayout>

😒在WelcomeActivity中写一个新线程,获取到用户登录状态

网络异常,图片无法展示
|

WelcomeActivity完整代码

publicclassWelcomeActivityextendsAppCompatActivity {

 

   @Override

   protectedvoidonCreate(BundlesavedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_welcome);

 

       Threadthread=newThread() {

           publicvoidrun() {

               super.run();

 

               try {

                   Thread.sleep(2000);

               } catch (InterruptedExceptione) {

                   e.printStackTrace();

               }

               //读取用户的登录状态

               SharedPreferencessp=getSharedPreferences("user", MODE_PRIVATE);

               BooleanisLogin=sp.getBoolean("isLogin", false);

                   //如果登录状态是true则直接进入用户首页

               if (isLogin) {

                   Intentintent=newIntent(WelcomeActivity.this, MainActivity.class);

                   startActivity(intent);

               } else {

                   //否则跳转到用户登录界面

                   Intentintent=newIntent(WelcomeActivity.this, LoginActivity.class);

                   startActivity(intent);

               }

           }

       };

       thread.start();

   }

}

😘运行效果

通过动图可以发现用户第一次登录时是需要验证身份的,而第二次登录用户可以直接进入”qq首页界面“,这是因为我们第一次登录的时候将用户的登录状态记录在了user文件里,再次登陆时WelcomeActivity代码中读取到了用户的登录状态是true,所以直接跳转到用户首页界面

网络异常,图片无法展示
|

那么这个user文件保存在哪里呢?

可以看到在data/data/项目包名shared_prefs文件夹下生成了一个user.xml的文件,里面存放就是我们的用户名和登录状态信息

网络异常,图片无法展示
|

网络异常,图片无法展示
|


相关文章
|
3月前
|
存储 安全 Android开发
Android数据存储:请解释ContentProvider是什么,它的主要作用是什么?
Android数据存储:请解释ContentProvider是什么,它的主要作用是什么?
25 0
|
3月前
|
存储 Java 数据库
Android数据存储:什么是Room Persistence Library?
Android数据存储:什么是Room Persistence Library?
42 0
|
3月前
|
存储 数据库连接 数据库
Android数据存储:解释SQLite数据库在Android中的使用。
Android数据存储:解释SQLite数据库在Android中的使用。
41 0
|
7月前
|
存储 数据库 文件存储
android 数据存储的知识点
android 数据存储的知识点
74 0
|
4月前
|
存储 XML Android开发
Android Studio App开发入门之数据存储中共享参数SharedPreferneces的讲解及使用(附源码 超详细必看)
Android Studio App开发入门之数据存储中共享参数SharedPreferneces的讲解及使用(附源码 超详细必看)
30 0
|
3月前
|
存储 XML 文件存储
Android数据存储:什么是SharedPreferences?它有什么用途?
Android数据存储:什么是SharedPreferences?它有什么用途?
75 0
|
4月前
|
存储 数据库 文件存储
android开发,使用kotlin学习数据存储
android开发,使用kotlin学习数据存储
58 0
|
4月前
|
存储 Android开发
Android SharedPreferences的使用
Android SharedPreferences的使用
30 0
|
8月前
|
Android开发
Android 中SharedPreferences的详解
Android 中SharedPreferences的详解
38 0
|
XML 存储 缓存