
很多时候我们开发的软件需要向用户提供软件参数设置功能,例如我们常用的QQ,用户可以设置是否允许陌生人添加自己为好友。对于软件配置参数的保存,如果是window软件通常我们会采用ini文件进行保存,如果是j2se应用,我们会采用propertIEs属性文件或者xml进行保存。如果是AndroID应用,我们最适合采用什么方式保存软件配置参数呢?AndroID平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数。使用SharedPreferences保存数据,其背后是用xml文件存放数据,文件存放在/data/data/<package name>/shared_prefs目录下:
SharedPreferences sharedPreferences = getSharedPreferences("ljq",Context.MODE_PRIVATE);Editor editor = sharedPreferences.edit();//获取编辑器editor.putString("name","林计钦");editor.putInt("age",24);editor.commit();//提交修改 生成的ljq.xml文件内容如下:
<?xml version='1.0' enCoding='utf-8' standalone='yes' ?><map> <string name="name">林计钦</string> <int name="age" value="24" /></map>
因为SharedPreferences背后是使用xml文件保存数据,getSharedPreferences(name,mode)方法的第一个参数用于指定该文件的名称,名称不用带后缀,后缀会由AndroID自动加上。方法的第二个参数指定文件的 *** 作模式,共有四种 *** 作模式,这四种模式前面介绍使用文件方式保存数据时已经讲解过。如果希望SharedPreferences背后使用的xml文件能被其他应用读和写,可以指定Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE权限。
另外Activity还提供了另一个getPreferences(mode)方法 *** 作SharedPreferences,这个方法默认使用当前类不带包名的类名作为文件的名称。
访问SharedPreferences中的数据
访问SharedPreferences中的数据代码如下:
SharedPreferences sharedPreferences = getSharedPreferences("ljq",Context.MODE_PRIVATE);//getString()第二个参数为缺省值,如果preference中不存在该key,将返回缺省值String name = sharedPreferences.getString("name","");int age = sharedPreferences.getInt("age",1);如果访问其他应用中的Preference,前提条件是:该preference创建时指定了Context.MODE_WORLD_READABLE或者Context.MODE_WORLD_WRITEABLE权限。
如:有个<package name>为com.ljq.action的应用使用下面语句创建了preference。
getSharedPreferences("ljq",Context.MODE_WORLD_READABLE);其他应用要访问上面应用的preference,首先需要创建上面应用的Context,然后通过Context 访问preference ,访问preference时会在应用所在包下的shared_prefs目录找到preference :
Context otherAppsContext = createPackageContext("com.ljq.action",Context.CONTEXT_IGnorE_Security);SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("ljq",Context.MODE_WORLD_READABLE);String name = sharedPreferences.getString("name",0);如果不通过创建Context访问其他应用的preference,也可以以读取xml文件方式直接访问其他应用preference对应的xml文件,如:
复制代码 代码如下:
file xmlfile = new file("/data/data/<package name>/shared_prefs/itcast.xml");//<package name>应替换成应用的包名
案例:
string.xml文件
<?xml version="1.0" enCoding="utf-8"?><resources> <string name="hello">Hello World,SpActivity!</string> <string name="app_name">软件配置参数</string> <string name="name">姓名</string> <string name="age">年龄</string> <string name="button">保存设置</string> <string name="showbutton">显示</string></resources>
main.xml布局文件
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="vertical" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent"> <relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/name" androID:textSize="20px" androID:ID="@+ID/nameLable" /> <EditText androID:layout_wIDth="80px" androID:layout_height="wrap_content" androID:layout_toRightOf="@ID/nameLable" androID:layout_aligntop="@ID/nameLable" androID:layout_marginleft="10px" androID:ID="@+ID/name" /> </relativeLayout> <relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textSize="20px" androID:text="@string/age" androID:ID="@+ID/ageLable" /> <EditText androID:layout_wIDth="80px" androID:layout_height="wrap_content" androID:layout_toRightOf="@ID/ageLable" androID:layout_aligntop="@ID/ageLable" androID:layout_marginleft="10px" androID:ID="@+ID/age" /> </relativeLayout> <relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"> <button androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/button" androID:ID="@+ID/button" /> <button androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/showbutton" androID:layout_toRightOf="@ID/button" androID:layout_aligntop="@ID/button" androID:ID="@+ID/showbutton" /> </relativeLayout> <TextVIEw androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:textSize="20px" androID:ID="@+ID/showtext" /></linearLayout>
package com.ljq.activity;import androID.app.Activity;import androID.content.Context;import androID.content.SharedPreferences;import androID.content.SharedPreferences.Editor;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.TextVIEw;import androID.Widget.Toast;public class SpActivity extends Activity { private EditText nameText; private EditText ageText; private TextVIEw resultText; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); nameText = (EditText)this.findVIEwByID(R.ID.name); ageText = (EditText)this.findVIEwByID(R.ID.age); resultText = (TextVIEw)this.findVIEwByID(R.ID.showtext); button button = (button)this.findVIEwByID(R.ID.button); button showbutton = (button)this.findVIEwByID(R.ID.showbutton); button.setonClickListener(Listener); showbutton.setonClickListener(Listener); // 回显 SharedPreferences sharedPreferences=getSharedPreferences("ljq123",Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE); String nameValue = sharedPreferences.getString("name",""); int ageValue = sharedPreferences.getInt("age",1); nameText.setText(nameValue); ageText.setText(String.valueOf(ageValue)); } private VIEw.OnClickListener Listener = new VIEw.OnClickListener(){ public voID onClick(VIEw v) { button button = (button)v; //ljq123文件存放在/data/data/<package name>/shared_prefs目录下 SharedPreferences sharedPreferences=getSharedPreferences("ljq123",Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE); switch (button.getID()) { case R.ID.button: String name = nameText.getText().toString(); int age = Integer.parseInt(ageText.getText().toString()); Editor editor = sharedPreferences.edit(); //获取编辑器 editor.putString("name",name); editor.putInt("age",age); editor.commit();//提交修改 Toast.makeText(SpActivity.this,"保存成功",Toast.LENGTH_LONG).show(); break; case R.ID.showbutton: String nameValue = sharedPreferences.getString("name",""); int ageValue = sharedPreferences.getInt("age",1); resultText.setText("姓名:" + nameValue + ",年龄:" + ageValue); break; } } };}运行结果
如何访问其他应用中的Preference
package com.ljq.sp;import java.io.file;import java.io.fileinputStream;import androID.content.Context;import androID.content.SharedPreferences;import androID.test.AndroIDTestCase;import androID.util.Log;public class AccessSharePreferenceTest extends AndroIDTestCase{ private static final String TAG = "AccessSharePreferenceTest"; /** * 访问SharePreference的方式一,注:权限要足够 * @throws Exception */ public voID testAccesspreference() throws Exception{ String path = "/data/data/com.ljq.activity/shared_prefs/ljq123.xml"; file file = new file(path); fileinputStream inputStream = new fileinputStream(file); //获取的是一个xml字符串 String data = new fileService().read(inputStream); Log.i(TAG,data); } /** * 访问SharePreference的方式二,注:权限要足够 * @throws Exception */ public voID testAccesspreference2() throws Exception{ Context context = this.getContext().createPackageContext("com.ljq.activity",Context.CONTEXT_IGnorE_Security); SharedPreferences sharedPreferences = context.getSharedPreferences("ljq123",Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE); String name = sharedPreferences.getString("name",""); int age = sharedPreferences.getInt("age",1); Log.i(TAG,name + " : " +age); }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的android使用SharedPreferences进行数据存储全部内容,希望文章能够帮你解决android使用SharedPreferences进行数据存储所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)