SharedPreferences
Android中数据存储有很多种方式,可以直接存在文件 数据库 网络,对于轻型数据,SharedPreferences是个很好的选择。
SharedPreferences本质是键值对的集合!
本次我将用一个小实例来简单描述SharedPreferences的使用过程。
- MySharedPrefence.java
package com.data.sharedpreference;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
/**
* SharedPrefences的簡單實用
*
* @author QT
*
*/
public class MySharedPrefence {
private static Context context;
public MySharedPrefence() {
// TODO Auto-generated constructor stub
}
public MySharedPrefence(Context context) {
this.context = context;
}
/**
* 運用sharedPerfences方式保存數據
*
* @param username
* @param password
* @return
*/
public static boolean savePrefence(String username, String password) {
boolean flag = false;
// 獲得SharedPreferences對象引用
SharedPreferences mySharedPreferences = context.getSharedPreferences(
"userInfo", Context.MODE_PRIVATE);
// 獲得編輯器
SharedPreferences.Editor myEditor = mySharedPreferences.edit();
myEditor.putString("username", username);
myEditor.putString("password", password);
// 置入值后,記得提交
myEditor.commit();
return true;
}
/**
* 獲取義haredPerfences方式保存的數據
*
* @return map集合
*/
public Map<String, Object> getPrefence() {
Map<String, Object> map = new HashMap<String, Object>();
//SharedPreferences的标志必须相同
SharedPreferences mySharedPreferences = context.getSharedPreferences(
"userInfo", Context.MODE_PRIVATE);
//根据键获取值,后一个参数为默认值,若不存在,则填充
String username = mySharedPreferences.getString("username",
"default name");
String password = mySharedPreferences.getString("password",
"default pass");
map.put("username", username);
map.put("password", password);
return map;
}
}
- SharedPrefenceTest.java
package com.data.sharedpreference;
import android.content.Context;
import android.test.AndroidTestCase;
import android.util.Log;
/**
* 测试文件
*
* @author QT
*
*/
public class SharedPrefenceTest extends AndroidTestCase {
public static String TAG = "SharedPrefenceTest";
public SharedPrefenceTest() {
// TODO Auto-generated constructor stub
}
/**
* 保存SharedPreferences的测试方法
*/
public void TestSavePreference() {
Context context = getContext();
MySharedPrefence mySharedPrefence = new MySharedPrefence(context);
boolean flag = mySharedPrefence.savePrefence("jay", "12345");
Log.i(TAG, "------->>>>---" + flag);
}
/**
* 获取SharedPreferences的测试方法
*/
public void TestGetPreference() {
Context context = getContext();
MySharedPrefence mySharedPrefence = new MySharedPrefence(context);
String message = mySharedPrefence.getPrefence().toString();
Log.i(TAG, "------->>>>---" + message);
}
}
- manifest.xml配置单元测试
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.data.sharedpreference"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="19" />
<!-- 添加单元测试 -->
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.data.sharedpreference" >
</instrumentation>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- 添加单元测试类库-->
<uses-library android:name="android.test.runner" />
<activity
android:name="com.data.sharedpreference.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
运行结果
- 保存SharedPreferences测试方法,保存在data/data,导出到桌面查看
- 得到SharedPreferences,并通过日志打印可得