一、Android数据存储方式
•1.文件
•2.SQLite数据库
•3.SharedPreferences(参数)
•4.内容提供者(Content Providers)
•5.网络
二、SharedPreferences简介:
—SharedPreferences是Android平台上一个轻量级的存储类,主要是保存一些常用的配置比如窗口状态,一般在Activity中 重载窗口状态onSaveInstanceState保存一般使用SharedPreferences完成,它提供了Android平台常规的Long长 整形、Int整形、String字符串型的保存,它是什么样的处理方式呢?
SharedPreferences
类似过去
Windows
系统上的
ini
配置文件,但是它分为多种权限,可以全局共享访问,android123提示最 终是以xml方式来保存,整体效率来看不是特别的高,对于常规的轻量级而言比SQLite要好不少,如果真的存储量不大可以考虑自己定义文件格式。xml 处理时Dalvik会通过自带底层的本地XML Parser解析,比如XMLpull方式,这样对于内存资源占用比较好。
—这种方式应该是用起来最简单的Android读写外部数据的方法了。他的用法基本上和J2SE(java.util.prefs.Preferences)中的用法一样,以一种简单、透明的方式来保存一些用户个性化设置的字体、颜色、位置等参数信息。一般的应用程序都会提供“设置”或者“首选项”的这样的界面,那么这些设置最后就可以 通过Preferences来保存,而程序员不需要知道它到底以什么形式保存的,保存在了什么地方。当然,如果你愿意保存其他的东西,也没有什么限制。只是在性能上不知道会有什么问题。
—
在
Android
系统中,这些信息以
XML
文件的形式保存在 /data/data/PACKAGE_NAME /shared_prefs 目录下。
三、获取SharedPreferences对象
1、用上下文(context)调用getSharedPreferences(),需要传入文件名称(保存为xml文件故不需要写后缀名)和文件操作权限,该方式获取的SharedPreferences对象可以被同一应用下的其它组件共享。
2、用Activity调用getPreferences(),只传入文件操作权限,会用不带包名的Activity的类名作为文件名保存为xml文件,该方法获取的SharedPreferences对象只能在当前Activity中使用。
1、用上下文(context)调用getSharedPreferences(),需要传入文件名称(保存为xml文件故不需要写后缀名)和文件操作权限,该方式获取的SharedPreferences对象可以被同一应用下的其它组件共享。
2、用Activity调用getPreferences(),只传入文件操作权限,会用不带包名的Activity的类名作为文件名保存为xml文件,该方法获取的SharedPreferences对象只能在当前Activity中使用。
四、SharedPreferences的四种操作模式:
Context.MODE_PRIVATE
:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
Context.MODE_APPEND
:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件.
Context.MODE_WORLD_READABLE
:表示当前文件可以被其他应用读取.
Context.MODE_WORLD_WRITEABLE
:表示当前文件可以被其他应用写入.
其中Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件.
五、操作。
SharePreferences存储数据是通过获取Editor编辑器对象来操作的。
插入数据:
调用Editor.putxxxx方法,两个参数分别为键和值。
获取数据:
调用Editor.getxxxx方法,两个参数分别为键和不存在指定键时的默认值。
删除数据:
调用Editor.remove方法,参数为指定的键。
清空所有数据:
调用Editor.clear方法
上述所有方法调用都要执行Editor.commit方法来提交。
插入数据:
调用Editor.putxxxx方法,两个参数分别为键和值。
获取数据:
调用Editor.getxxxx方法,两个参数分别为键和不存在指定键时的默认值。
删除数据:
调用Editor.remove方法,参数为指定的键。
清空所有数据:
调用Editor.clear方法
上述所有方法调用都要执行Editor.commit方法来提交。
六、存储数据
//获取SharedPreferences对象
SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
//通过SharedPreferences来获取Editor编辑器存入数据
Editor editor = preferences.edit();
editor.putString("name", name);
editor.putInt("age", age);
//最后需要调用commit把要保存的数据提交
editor.commit();
七、读取信息:
//如果没有获取到信息,则用第二个参数中的值
String name = pres.getString(“name”, “NO”);
int age = pres.getInt("age", 0);
String bir = pres.getString("bir", "NO");
txtResult.setText("Name="+name+";age="+age+";bir="+bir);
八、文件
事例
SharedPreferences_MainAct.java文件
//Android数据存储方式-SharedPreferences
//SharedPreferences是一个轻量级的存储类,适合用于保存软件配置参数。
/**
*
<!-- com.test.FileStream -->
<activity
android:name="com.test.SharedPreferences.SharedPreferences_MainAct"
android:label="@string/app_name">
<intent-filter >
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
*/
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class SharedPreferences_MainAct extends Activity {
private EditText sharedpreferences_nameET;
private EditText sharedpreferences_ageET;
private Sharedpreferences_Service service;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sharedpreferences_mainact);
sharedpreferences_nameET = (EditText) findViewById(R.id.sharedpreferences_nameET);
sharedpreferences_ageET = (EditText) findViewById(R.id.sharedpreferences_ageET);
service = new Sharedpreferences_Service(this);
}
public void set(View view){
String name = sharedpreferences_nameET.getText().toString();
int age = Integer.parseInt(sharedpreferences_ageET.getText().toString());
service.set(name, age);
Toast.makeText(this, "save success", Toast.LENGTH_SHORT).show();
}
public void get(View view){
Map<String, String> params = service.get();
String name = params.get("name");
int age = Integer.parseInt(params.get("age"));
Toast.makeText(this, "getinfor:"+name+age, Toast.LENGTH_SHORT).show();
}
}
Sharedpreferences_Service.java文件
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.AvoidXfermode.Mode;
public class Sharedpreferences_Service {
private Context context;
private String fileName = "mypreferences";
public Sharedpreferences_Service(Context context) {
this.context = context;
}
/**
* 保存参数
* @param name
* @param age
*/
public void set(String name, int age){
/**
* 获取SharedPreferences对象
* 1、用上下文调用getSharedPreferences,需要传入文件名称(保存为xml文件故不需要写后缀名)和文件操作权限
* 2、用Activity调用getPreferences,只传入文件操作权限,会用不带包名的Activity的类名作为文件名保存为xml文件
*/
SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
//通过SharedPreferences来获取Editor编辑器存入数据
Editor editor = preferences.edit();
editor.putString("name", name);
editor.putInt("age", age);
//最后需要调用commit把要保存的数据提交
editor.commit();
}
/**
* 读取参数
* @return
*/
public Map<String, String> get(){
SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
String name = preferences.getString("name", "");
int age = preferences.getInt("age", 0);
Map<String, String> params = new HashMap<String, String>();
params.put("name", name);
params.put("age", String.valueOf(age));
return params;
}
}
sharedpreferences_mainact.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"/>
<EditText
android:id="@+id/sharedpreferences_nameET"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="age"/>
<EditText
android:id="@+id/sharedpreferences_ageET"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="set"
android:text="set"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="get"
android:text="get"/>
</LinearLayout>
</LinearLayout>
关于SharedPreferences的封装:
import android.content.Context;
import android.content.SharedPreferences;
public class UtilsPreferences {
public static String PREFERENCE_NAME = "filename";
public static boolean putString(Context context, String key, String value) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString(key, value);
return editor.commit();
}
public static String getString(Context context, String key) {
return getString(context, key, null);
}
public static String getString(Context context, String key, String defaultValue) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
return settings.getString(key, defaultValue);
}
public static boolean putInt(Context context, String key, int value) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putInt(key, value);
return editor.commit();
}
public static int getInt(Context context, String key) {
return getInt(context, key, -1);
}
public static int getInt(Context context, String key, int defaultValue) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
return settings.getInt(key, defaultValue);
}
public static boolean putLong(Context context, String key, long value) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putLong(key, value);
return editor.commit();
}
public static long getLong(Context context, String key) {
return getLong(context, key, -1);
}
public static long getLong(Context context, String key, long defaultValue) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
return settings.getLong(key, defaultValue);
}
public static boolean putFloat(Context context, String key, float value) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putFloat(key, value);
return editor.commit();
}
public static float getFloat(Context context, String key) {
return getFloat(context, key, -1);
}
public static float getFloat(Context context, String key, float defaultValue) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
return settings.getFloat(key, defaultValue);
}
public static boolean putBoolean(Context context, String key, boolean value) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(key, value);
return editor.commit();
}
public static boolean getBoolean(Context context, String key) {
return getBoolean(context, key, false);
}
public static boolean getBoolean(Context context, String key, boolean defaultValue) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
return settings.getBoolean(key, defaultValue);
}
//----------------------------------------------------
private final static int mValue_String = 1;
private final static int mValue_Int = 2;
private final static int mValue_Float = 3;
private final static int mValue_Long = 4;
private final static int mValue_Boolean = 5;
private final static int mValue_Max = mValue_Boolean+1;
private static int mValue_curr = mValue_Max;
public static boolean putString_(Context context, String key, String value) {
mValue_curr = mValue_String;
return putValue(context, key, value);
}
public static String getString_(Context context, String key) {
return getString(context, key, null);
}
public static String getString_(Context context, String key, String defaultValue) {
mValue_curr = mValue_String;
return (String)getValue(context, key, defaultValue);
}
public static boolean putInt_(Context context, String key, int value) {
mValue_curr = mValue_Int;
return putValue(context, key, value);
}
public static int getInt_(Context context, String key) {
return getInt(context, key, -1);
}
public static int getInt_(Context context, String key, int defaultValue) {
mValue_curr = mValue_Int;
return (Integer)getValue(context, key, defaultValue);
}
public static boolean putFloat_(Context context, String key, float value) {
mValue_curr = mValue_Float;
return putValue(context, key, value);
}
public static float getFloat_(Context context, String key) {
return getFloat(context, key, -1);
}
public static float getFloat_(Context context, String key, float defaultValue) {
mValue_curr = mValue_Float;
return (Float)getValue(context, key, defaultValue);
}
public static boolean putLong_(Context context, String key, long value) {
mValue_curr = mValue_Long;
return putValue(context, key, value);
}
public static long getLong_(Context context, String key) {
return getLong(context, key, -1);
}
public static long getLong_(Context context, String key, long defaultValue) {
mValue_curr = mValue_Long;
return (Long)getValue(context, key, defaultValue);
}
public static boolean putBoolean_(Context context, String key, boolean value) {
mValue_curr = mValue_Boolean;
return putValue(context, key, value);
}
public static boolean getBoolean_(Context context, String key) {
return getBoolean(context, key, false);
}
public static boolean getBoolean_(Context context, String key, boolean defaultValue) {
mValue_curr = mValue_Boolean;
return (Boolean)getValue(context, key, defaultValue);
}
private static boolean putValue(Context context, String key, Object value)
{
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
switch (mValue_curr)
{
case mValue_String:
editor.putString(key, (String)value);
break;
case mValue_Int:
editor.putInt(key, (Integer)value);
break;
case mValue_Float:
editor.putFloat(key, (Float)value);
break;
case mValue_Long:
editor.putLong(key, (Long)value);
break;
case mValue_Boolean:
editor.putBoolean(key, (Boolean)value);
break;
default:
break;
}
return editor.commit();
}
private static Object getValue(Context context, String key, Object defaultValue)
{
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
switch (mValue_curr)
{
case mValue_String:
return settings.getString(key, (String)defaultValue);
case mValue_Int:
return settings.getInt(key, (Integer)defaultValue);
case mValue_Float:
return settings.getFloat(key, (Float)defaultValue);
case mValue_Long:
return settings.getLong(key, (Long)defaultValue);
case mValue_Boolean:
return settings.getBoolean(key, (Boolean)defaultValue);
default:
break;
}
return null;
}
//----------------------------------------------------
}