当前位置: 移动技术网 > IT编程>移动开发>Android > Android SharedPreferences存储用法详解

Android SharedPreferences存储用法详解

2019年07月24日  | 移动技术网IT编程  | 我要评论

河南省公务员考试,万裕国际影城,家有儿女之疯狂假期

先看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下载地址:

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网