[功能]

大家在android开发中 一点有这样的需求 就是需要保存一下与该程序有关的属性设置的问题

比如:window xp 中 <假设系统盘为 C:/> 的位置为: C:\Program Files

那么在android中是怎样呢? 那就是:SharedPreferences

既然它是用来保存数据的 那么一点下面问题:

1. 如何创建

2. 如何加入数据

3. 如何取出数据

因为 很多程序都有这个需要 所以自己把该功能集成并列出一些接口函数 以后用的话 会方便很多 这个类名为:SharedPreferencesHelper

[代码]

1. 以指定名字来创建

  1. SharedPreferences sp;
  2. SharedPreferences.Editor editor;
  3. Context context;
  4. public SharedPreferencesHelper(Context c,String name){
  5. context = c;
  6. sp = context.getSharedPreferences(name, 0);
  7. editor = sp.edit();
  8. }

2. 以键值<String Key,String Value> 的方式加入数据

  1. public void putValue(String key, String value){
  2. editor = sp.edit();
  3. editor.putString(key, value);
  4. editor.commit();
  5. }

3. 以 String Key 为索引来取出数据

  1. public String getValue(String key){
  2. return sp.getString(key, null);
  3. }

4. 如何使用 SharedPreferencesHelper

  1. package com.android.SharedPreferences;
  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.widget.TextView;
  1. /*
  2. * to access from: data/data/com.android.SharedPreferences/share_prefs
  3. */
  4. public class SharedPreferencesUsage extends Activity {
  5. public final static String COLUMN_NAME ="name";
  6. public final static String COLUMN_MOBILE ="mobile";
  7. SharedPreferencesHelper sp;
  8. /** Called when the activity is first created. */
  9. @Override
  10. public void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. //setContentView(R.layout.main);
  13. sp = new SharedPreferencesHelper(this, "contacts");
  14. //1. to store some value
  15. sp.putValue(COLUMN_NAME, "Gryphone");
  16. sp.putValue(COLUMN_MOBILE, "123456789");
  17. //2. to fetch the value
  18. String name = sp.getValue(COLUMN_NAME);
  19. String mobile = sp.getValue(COLUMN_MOBILE);
  20. TextView tv = new TextView(this);
  21. tv.setText("NAME:"+ name + "\n" + "MOBILE:" + mobile);
  22. setContentView(tv);
  23. }
  24. }

5. 其他问题

* 文件存放路径: 因为我的这个例子的pack_name 为:package com.android.SharedPreferences;

所以存放路径为:data/data/com.android.SharedPreferences/share_prefs/contacts.xml

* contacts.xml 的内容为:

  1. <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
  2. <map>
  3. <string name="mobile">123456789</string>
  4. <string name="name">Gryphone</string>
  5. </map>

* 取出的数据为:

转载自: http://www.iteye.com/topic/573212

SharedPreferences(转)的更多相关文章

  1. Android之SharedPreferences数据存储

    一.SharedPreferences保存数据介绍 如果有想要保存的相对较小键值集合,应使用SharedPreferences API.SharedPreferences对象指向包含键值对的文件并提供 ...

  2. SharedPreferences 的另一种场景的用法

    SharedPreferences 的另一种场景的用法 昨天,下班在家想做什么来着,然后想用SharedPreferences存点数据,但是不知道咋地突然想到,SharedPreferences是应用 ...

  3. 取代SharedPreferences的多进程解决方案

    Android的SharedPreferences用来存储一些键值对, 但是却不支持跨进程使用. 跨进程来用的话, 当然是放在数据库更可靠啦, 本文主要是给作者的新库PreferencesProvid ...

  4. 简单的学习心得:网易云课堂Android开发第五章SharedPreferences与文件管理

    一.SharedPreferences (1)SharedPreferences能够用来保存一些属于基本数据类型的数据. (2)保存数据,删除数据都是由SharedPreferences的内部接口Ed ...

  5. android SharedPreferences 存储对象

    我们知道SharedPreferences只能保存简单类型的数据,例如,String.int等. 如果想用SharedPreferences存取更复杂的数据类型(类.图像等),就需要对这些数据进行编码 ...

  6. Android SharedPreferences公共类sharedhelper

    SimpAndroidFarme是近期脑子突然发热想做的android快速开发的框架,目标是模块化 常用的控件,方便新手学习和使用.也欢迎老鸟来一起充实项目:项目地址 sharedpreference ...

  7. Android SharedPreferences存储

    原创文章,转载请注明出处:http://www.cnblogs.com/baipengzhan/p/Android_SharedPreferences.html 一 概念 SharedPreferen ...

  8. Android——SharedPreferences

    SharedPreferences是一种轻型的Android数据存储方式,用来保存应用的一些常用配置. 实现SharedPreferences存储的步骤如下: 1.根据Context获取SharedP ...

  9. SharedPreferences 详解

    获取SharedPreferences的两种方式: 1 调用Context对象的getSharedPreferences()方法 2 调用Activity对象的getPreferences()方法 两 ...

  10. Tray - a SharedPreferences replacement for Android

    一个代替SharedPreferences的开源库, no Editor, no commit() no apply(),因此不存在UI卡顿现象,并且支持多线程,在一个线程中存另一个线程中取数据. h ...

随机推荐

  1. C#里List.Sort的用法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace List ...

  2. 转<<C#集合Dictionary中按值的降序排列

    转载地址:http://blog.sina.com.cn/s/blog_5c5bc9070100pped.html C#集合Dictionary中按值的降序排列 static void Main(st ...

  3. PHP数字格式化,每三位逗号分隔数字,可以保留小数

    在报价的时候为了给浏览者更清晰明确的数字,所以需要用到数字格式化,有两种方法,一种自己写函数,另一种当然是系统自带的,其实我更喜欢系统自带的. 先来系统简单的: string number_forma ...

  4. Android EditText的设置

    1.输入法Enter键图标的设置: 软件盘的界面替换只有一个属性android:imeOptions,这个属性的可以取的值有normal,actionUnspecified,actionNone,ac ...

  5. @version ||= version

    # -*- encoding : utf-8 -*- class InterfaceBaseController < ActionController::Base private def set ...

  6. JS匿名函数的理解

    js匿名函数的代码如下:(function(){ // 这里忽略jQuery 所有实现 })(); 半年前初次接触jQuery 的时候,我也像其他人一样很兴奋地想看看源码是什么样的.然而,在看到源码的 ...

  7. 部署Office Web Apps Server并配置其与SharePoint 2013的集成

    部署Office Web Apps Server并配置其与SharePoint 2013的集成   Office Web Apps Server 是新的 Office 服务器产品,它提供 Word.P ...

  8. css调用外部样式和css样式说明剧中显示

    <title>边走边乔</title><link href="css/style.css" rel="stylesheet" ty ...

  9. 算法:comparable比较器的排序原理实现(二叉树中序排序)

    Comparable比较器排序远离实现 package test.java.api.api13; /** * 手工实现二叉树的比较算法: 第一遍感觉很神秘,但是真正自己写下来,就感觉很简单,理解就好: ...

  10. CPinyin unicode汉字查找拼音(支持多音字)

    下载代码 --------------------------------------------------------------------------------- 虽然很笨的办法,却非常有效 ...