sharepreference: 存储软件的一些功能属性
采用xml文件存储数据(以Map存储(KEY ,VALUE))
存储用户使用偏好:data/data/shared_pres/xxx.xml存储
PreferencesService.java
package com.hisoft.service; import java.util.HashMap; import java.util.Map; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; public class PreferencesService { private Context context; SharedPreferences preferences; public PreferencesService(Context context) { this.context = context; preferences = context.getSharedPreferences("soft", context.MODE_PRIVATE); //存储文件的设置“soft”文件名
//context.MODE_PRIVATE 文件操作模式 } public void save(String name_str, Integer age) { Editor editor = preferences.edit(); //获得编辑其向其中写入数据 editor.putString("name", name_str); //存入数据 editor.putInt("age", age); editor.commit(); //提交保存 } public Map<String, String> getPreferences() { Map<String, String> map = new HashMap<String, String>(); map.put("name", preferences.getString("name", "")); //直接使用get类型(“KEY”,default value) map.put("age", String.valueOf(preferences.getInt("age", 0))); return map; } }
2.soft.xml
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <int name="age" value="27" /> <string name="name">123</string> </map>