Android SharedPreferences存储用法详解

 更新时间:2017年02月24日 17:03:07   作者:段炼Android  
这篇文章主要为大家详细介绍了Android SharedPreferences存储用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

先看Demo运行效果

SharedPreferences详解

SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activity暂停时,将此activity的状态保存到SharedPereferences中;当Activity重载,系统回调方法onSaveInstanceState时,再从SharedPreferences中将值取出.

SharedPreferences提供了java常规的Long、Int、String等类型数据的保存接口.

SharedPreferences类似过去Windows系统上的ini配置文件,但是它分为多种权限,可以全局共享访问。

提示最终是以xml方式来保存,整体效率来看不是特别的高,对于常规的轻量级而言比SQLite要好不少,如果真的存储量不大可以考虑自己定义文件格式。xml处理时Dalvik会通过自带底层的本地XML Parser解析,比如XMLpull方式,这样对于内存资源占用比较好.

SharedPreferences数据的四种操作模式

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

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

SharedPreferences使用步骤

SharedPreferences的使用非常简单,使用SharedPreferences保存key-value对的步骤如下:

  (1)使用Activity类的getSharedPreferences方法获得SharedPreferences对象,其中存储key-value的文件的名称由getSharedPreferences方法的第一个参数指定.
  (2)使用SharedPreferences接口的edit获得SharedPreferences.Editor对象.
  (3)通过SharedPreferences.Editor接口的putXxx方法保存key-value对。其中Xxx表示不同的数据类型。例如:字符串类型的value需要用putString方法.
  (4)通过SharedPreferences.Editor接口的commit方法保存key-value对。commit方法相当于数据库事务中的提交(commit)操作.

具体代码的书写流程为:

 A、存放数据信息

1、打开Preferences,名称为config,如果存在则打开它,否则创建新的Preferences
SharedPreferencesconfig= getSharedPreferences(“config”, 0);
2、让config处于编辑状态
SharedPreferences.Editor editor =config.edit();
3、存放数据
editor.putString(“name”,”ATAAW”);
editor.putString(“URL”,”ATAAW.COM”);
4、完成提交
editor.commit();

B、读取数据信息
1、获取Preferences
SharedPreferencesconfig= getSharedPreferences(“config”, 0);
2、取出数据
String name =config.getString(“name”,”默认值”);
String url = config.getString(“URL”,”default”);
以上就是Android中SharedPreferences的使用方法 

demo的实现

MainActivity布局文件

<?xml version="1.0" encoding="utf-8"?> 
<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="com.duanlian.sharedpreferencesdemo.MainActivity"> 
 
 <LinearLayout 
 android:layout_width="match_parent" 
 android:layout_height="50dp"> 
 
 <TextView 
  android:layout_width="wrap_content" 
  android:layout_height="match_parent" 
  android:gravity="center" 
  android:text="用户名" 
  android:textSize="23sp" /> 
 
 <EditText 
  android:id="@+id/username" 
  android:layout_width="0dp" 
  android:layout_height="wrap_content" 
  android:layout_weight="1" /> 
 </LinearLayout> 
 <LinearLayout 
 android:layout_width="match_parent" 
 android:layout_height="50dp"> 
 
 <TextView 
  android:layout_width="wrap_content" 
  android:layout_height="match_parent" 
  android:gravity="center" 
  android:text="密 码" 
  android:textSize="23sp" /> 
 
 <EditText 
  android:id="@+id/password" 
  android:layout_width="0dp" 
  android:layout_height="wrap_content" 
  android:layout_weight="1" /> 
 </LinearLayout> 
 <Button 
 android:onClick="save" 
 android:layout_width="match_parent" 
 android:layout_height="50dp" 
 android:text="保存信息"/> 
 <Button 
 android:onClick="change" 
 android:layout_width="match_parent" 
 android:layout_height="50dp" 
 android:text="跳转"/> 
</LinearLayout> 

ActivityA的布局文件

<?xml version="1.0" encoding="utf-8"?> 
<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="com.duanlian.sharedpreferencesdemo.MainActivity"> 
 
 <LinearLayout 
 android:layout_width="match_parent" 
 android:layout_height="50dp"> 
 
 <TextView 
  android:layout_width="wrap_content" 
  android:layout_height="match_parent" 
  android:gravity="center" 
  android:text="用户名" 
  android:textSize="23sp" /> 
 
 <EditText 
  android:id="@+id/username" 
  android:layout_width="0dp" 
  android:layout_height="wrap_content" 
  android:layout_weight="1" /> 
 </LinearLayout> 
 <LinearLayout 
 android:layout_width="match_parent" 
 android:layout_height="50dp"> 
 
 <TextView 
  android:layout_width="wrap_content" 
  android:layout_height="match_parent" 
  android:gravity="center" 
  android:text="密 码" 
  android:textSize="23sp" /> 
 
 <EditText 
  android:id="@+id/password" 
  android:layout_width="0dp" 
  android:layout_height="wrap_content" 
  android:layout_weight="1" /> 
 </LinearLayout> 
 <Button 
 android:onClick="get" 
 android:layout_width="match_parent" 
 android:layout_height="50dp" 
 android:text="得到信息"/> 
</LinearLayout> 

MainActivity里存储的具体实现
都是按照上面写的步骤来实现的,最后一定要提交

package com.duanlian.sharedpreferencesdemo; 
 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
 
public class MainActivity extends AppCompatActivity { 
 
 private EditText passWors; 
 private EditText userName; 
 private SharedPreferences preferences; 
 private SharedPreferences.Editor editor; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 initView(); 
 } 
 
 private void initView() { 
 userName = (EditText) findViewById(R.id.username); 
 passWors = (EditText) findViewById(R.id.password); 
 //1,实例化SharedPreferences对象,参数1:保存的名字,参数2:保存的模式MODE_PRIVATE私有的 
 preferences = getSharedPreferences("config", MODE_PRIVATE); 
 //2,让SharedPreferences处于可编辑状态 
 editor = preferences.edit(); 
 
 } 
 
 /** 
 * 保存按钮的监听 
 * @param view 
 */ 
 public void save(View view) { 
 //拿到用户输入的数据 
 String name = userName.getText().toString().trim(); 
 String pwd = passWors.getText().toString().trim(); 
 //3,储存数据,类似于map 
 editor.putString("name", name); 
 editor.putString("pwd", pwd); 
 //4,提交 
 editor.commit(); 
 
 } 
 
 /** 
 * 跳转的点击监听 
 * @param view 
 */ 
 public void change(View view) { 
 Intent intent = new Intent(MainActivity.this, ActivityA.class); 
 startActivity(intent); 
 
 } 
} 

取出数据的实现

package com.duanlian.sharedpreferencesdemo; 
 
import android.content.SharedPreferences; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
 
public class ActivityA extends AppCompatActivity { 
 
 private EditText userName; 
 private EditText passWord; 
 private String name; 
 private String pwd; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_a); 
 //得到数据 
 //1,得到SharedPreferences对象 
 SharedPreferences preferences = getSharedPreferences("config", 0); 
 //2,取出数据 
  name = preferences.getString("name",""); 
  pwd = preferences.getString("pwd", ""); 
 userName = (EditText) findViewById(R.id.username); 
 passWord = (EditText) findViewById(R.id.password); 
 
 } 
 
 /** 
 * 得到数据按钮的监听 
 * @param view 
 */ 
 public void get(View view) { 
 userName.setText(name); 
 passWord.setText(pwd); 
 } 
} 

demo下载地址:SharedPreferences

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android中TextView显示圆圈背景或设置圆角的方法

    Android中TextView显示圆圈背景或设置圆角的方法

    TextView显示文本给用户,并允许他们选择编辑。TextView是一个完整的文本编辑器,但是其基本类配置为不允许编辑。下面这篇文章主要给大家介绍了关于Android中TextView显示圆圈背景或设置圆角的方法,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-05-05
  • 解析Android中View转换为Bitmap及getDrawingCache=null的解决方法

    解析Android中View转换为Bitmap及getDrawingCache=null的解决方法

    在android中经常会遇到View转换为Bitmap的情形,本篇文章主要介绍了Android中View转换为Bitmap及getDrawingCache=null的解决方法,有需要的可以了解一下。
    2016-11-11
  • Android不规则图像填充颜色小游戏

    Android不规则图像填充颜色小游戏

    这篇文章主要介绍了Android不规则图像填充颜色小游戏,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧
    2018-05-05
  • 浅析Android录屏 MediaRecorder

    浅析Android录屏 MediaRecorder

    MediaRecorder类是Android sdk提供的一个专门用于音视频录制,一般利用手机麦克风采集音频,摄像头采集图片信息。这篇文章主要介绍了Android录屏 MediaRecorder介绍,需要的朋友可以参考下
    2020-03-03
  • Android快速实现无预览拍照功能

    Android快速实现无预览拍照功能

    这篇文章主要为大家详细介绍了Android快速实现无预览拍照功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-06-06
  • Android实现手势密码功能

    Android实现手势密码功能

    这篇文章主要为大家详细介绍了Android实现手势密码功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • Android BroadcastReceiver接收收到短信的广播

    Android BroadcastReceiver接收收到短信的广播

    这篇文章主要为大家详细介绍了Android BroadcastReceiver接收收到短信的广播,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • 基于移动端真机调试的图文教程(分享)

    基于移动端真机调试的图文教程(分享)

    下面小编就为大家分享一篇基于移动端真机调试的图文教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12
  • Android自定义view实现滑动解锁九宫格控件

    Android自定义view实现滑动解锁九宫格控件

    这篇文章主要介绍了Android自定义view实现滑动解锁九宫格控件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2023-02-02
  • 详解Flutter如何获取Text截断后的字符串

    详解Flutter如何获取Text截断后的字符串

    当Text文本设置maxLins属性将文本强制截断之后,Text的承载字符串是截断前,还是截断后的呢,我们又该如何获取截断后的字符串呢,下面就来和大家详细讲讲
    2023-06-06

最新评论