标签:
安卓应用将数据存储在本地的方式一共有四种,按照轻量级到重量级的顺序分别是:
1、用SharedPreferences存储数据
2、用ContentProvider存储数据
3、用SQLite存储数据
4、用本地文件存储数据
如果加上用网络存储数据,一共就有五种了,首先我们来看第一种SharedPreference:
一、interface android.content.SharedPreferences
它的属性类似于早期windows的ini配置文件,最终是以xml方式来保存,你也可以理解为一个小型的key-value数据库。
SharedPreference本质上是一个接口,它是通过调用Dalvik底层XMLParser处理XML的,资源占用貌似不是特别大。让我们来看看android SDK的文档里怎么说:
Interface for accessing and modifying preference data returned byContext.getSharedPreferences
. For any particular set of preferences, there is a single instance of this class that all clients share. Modifications to the preferences must go through anEditor
object to ensure the preference values remain in a consistent state and control when they are committed to storage. Objects that are returned from the variousget
methods must be treated as immutable by the application.
Note: currently this class does not support use across multiple processes. This will be added later.
翻译一下:
它是一个用于访问和更改由Context.getSharedPrenference返回的偏好(或首选项、属性、配置,随便你怎么说,)的接口。对于任意一组特定的属性,有一个所有客户端共享的单例。如果你想更改这些属性,你必须通过一个Editor(SharedPreferences的内部类)对象来保证属性值在被提交到存储设备的时候,他们能保持在一致和受控的状态。程序必须以immutable(不可变)方式处理以get方法返回的对象。
备注:这个类当前不支持跨进程访问,这个特性以后会增加。
使用SharedPreferences的四个步骤:
1、得到Context对象
2、用Context对象的getSharedPreferences方法得到一个SharedPreference对象,下面是android SDK 文档:
public abstract SharedPreferences getSharedPreferences (String name, int mode)
Retrieve and hold the contents of the preferences file ‘name‘, returning a SharedPreferences through which you can retrieve and modify its values. Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other‘s edits as soon as they are made.
Parameters
name Desired preferences file. If a preferences file by this name does not exist, it will be created when you retrieve an editor (SharedPreferences.edit()) and then commit changes (Editor.commit()). mode Operating mode. Use 0 or MODE_PRIVATE
for the default operation,MODE_WORLD_READABLE
andMODE_WORLD_WRITEABLE
to control permissions.Returns
- The single
SharedPreferences
instance that can be used to retrieve and modify the preference values.
3、用SharedPreference对象的edit()方法得到一个Editor对象(如果不需要写,这一步可以省略)
4、得到某一个具体的preference属性值
5、用editor.putXXX()方法修改属性值,XXX是类型名(如Long、Int、String等)(如果不需要写,这一步可以省略)
6、用editor.commit()方法提交更改(如果不需要写,这一步可以省略)
下面是示例代码:
import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; /** * @author Chandler public class TestPref extends Activity { SharedPreferences mySharedPreferences; Editor editor; long oldvalue,newvalue = 1234; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_interval_compare); //得到Context对象 Context ctx = TestPref.this; //得到SharedPreference对象,test是属性文件名 mySharedPreferences = ctx.getSharedPreferences("test", MODE_PRIVATE); //得到editor对象 editor = mySharedPreferences.edit(); //读取文件中存储的老值,如果不存在,返回-1 oldValue = mySharedPreferences.getLong("oldValue",-1); //在Logcat中输出读取的oldValue值 System.out.println(oldValue); } //退出时保存 @Override protected void onDestroy(){ super.onDestroy(); //将long newValue存放到oldValue中去 editor.putLong("oldValue", newValue); //提交更改 editor.commit(); Log.d(TAG, "----onDestroy----"); } }
标签:
原文地址:http://blog.csdn.net/u010760567/article/details/51351300