写应用程序时,一般把一些配置写在xml文件里,方便配置!然后在应用启动的时(在继承的application里)初始化配置
//在application里做的初始化工作 // 初始化设置 SettingUtility.setSettingUtility();
使用xml里的配置
<span style="white-space:pre"> </span>Logger.DEBUG = SettingUtility.getBooleanSetting("debug"); /** * 程序的文件目录,如果setting配置的是android,标志目录位于/sdcard/Application/PackageName/...下<br/> * 否则,就是/sdcard/setting[root_path]/...目录 * * @return */ public String getAppPath() { if ("android".equals(SettingUtility.getStringSetting("root_path"))) return getExternalCacheDir().getAbsolutePath() + File.separator; return SdcardUtils.getSdcardPath() + File.separator + SettingUtility.getStringSetting("root_path") + File.separator; }
public class SettingUtility { private static Map<String,Setting> settingMap; static { settingMap = new HashMap<String,Setting>(); } private SettingUtility() { } public static void setSettingUtility() { addSettings("settings"); <span style="white-space:pre"> </span>/** <span style="white-space:pre"> </span>*主要是一些初始化代码 if (SdcardUtils.hasSdcardAndCanWrite()) { File rootFile = new File(GlobalContext.getInstance().getAppPath()); if (!rootFile.exists()) rootFile.mkdirs(); // 数据缓存目录设置 File jsonFile = new File(rootFile.getAbsolutePath() + File.separator + getPermanentSettingAsStr("com_m_common_json","files")); if (!jsonFile.exists()) jsonFile.mkdirs(); // 缓存目录设置 File imageFile = new File(rootFile.getAbsolutePath() + File.separator + getPermanentSettingAsStr("com_m_common_image","images")); if (!imageFile.exists()) imageFile.mkdirs(); }*/ } /** * 添加设置配置数据 * * @param settingsXmlName */ public static void addSettings(String settingsXmlName) { Map<String,Setting> newSettingMap = SettingsXmlParser.parseSettings(GlobalContext.getInstance(),settingsXmlName); Set<String> keySet = newSettingMap.keySet(); for (String key : keySet) settingMap.put(key,newSettingMap.get(key)); } public static boolean getBooleanSetting(String type) { if (settingMap.containsKey(type)) return Boolean.parseBoolean(settingMap.get(type).getValue()); return false; } public static int getIntSetting(String type) { if (settingMap.containsKey(type)) return Integer.parseInt(settingMap.get(type).getValue()); return -1; } public static String getStringSetting(String type) { if (settingMap.containsKey(type)) return settingMap.get(type).getValue(); return null; } public static Setting getSetting(String type) { if (settingMap.containsKey(type)) return settingMap.get(type); return null; }
1、放在res下的raw的xml资源文件 setting.xml
<?xml version="1.0" encoding="UTF-8"?> <app-settings> <setting-array type="root_path" index="0"> <des>应用程序相对sdcard的相对路径目录,所有的文件都存储在这个文件夹中</des> <setting> <value>android</value> </setting> </setting-array> <setting-array type="com_m_common_json" index="0"> <des>业务数据sdcard缓存路径,相对root_path路径</des> <setting> <value>app_data</value> </setting> </setting-array> <setting-array type="com_m_common_image" index="0"> <des>图片数据sdcard缓存路径,相对root_path路径</des> <setting> <value>app_image</value> </setting> </setting-array> <setting-array type="draft" index="0"> <des>草稿图片,相对root_path路径</des> <setting> <value>draft</value> </setting> </setting-array> <setting type="saveImage"> <des>我收藏的图片</des> <value>myPics</value> </setting> <setting type="crashlogenable"> <des>错误日志记录</des> <value>true</value> </setting> <setting type="crashlog"> <des>错误日志目录</des> <value>crashlog</value> </setting> <setting-array type="cache_utility" index="2"> <des>缓存模式设置</des> <setting> <des>DB数据缓存</des> <value>org.aisen.android.support.cache.DBCacheUtility</value> </setting> <setting> <des>文件数据缓存</des> <value>com.sysweal.marketing.support.cache.FileCacheUtility_2</value> </setting> </setting-array> <setting type="memory_cache_validity"> <des>内存缓存数据有效期,等于0为不保持内存数据,单位(s)</des> <value>0</value> </setting> <setting type="http"> <value>org.aisen.android.network.http.DefHttpUtility</value> </setting> <setting type="cache_validtime"> <des>缓存数据的有效期,max_date为永久有效,单位(s)</des> <value>max_date</value> </setting> <setting type="debug"> <des>APP是否是DEBUG模式</des> <value>false</value> </setting> <setting type="cache_enable"> <des>缓存开关,在这里设置无效,需要在action.xml中覆盖这个设置</des> <value>true</value> </setting> <setting type="page_count"> <des>分页大小</des> <value>50</value> </setting> <setting-array type="app_channel" index="0"> <des>应用渠道</des> <setting> <value>test</value> </setting> <setting> <value>wandoujia</value> </setting> <setting> <value>googleplay</value> </setting> </setting-array> <setting type="bd_debug"> <des>百度移动统计debug</des> <value>false</value> </setting> <setting type="image_suffix"> <des>图片缓存的后缀</des> <value>auto</value> </setting> <setting type="about_url"> <des>关于</des> <value>https://github.com/wangdan/AisenWeiBo/blob/master/README.md</value> </setting> <setting type="help_url"> <des>帮助</des> <value>https://github.com/wangdan/AisenWeiBo/blob/master/HELP.md</value> </setting> <!-- <setting type="fragment_lifecycle"> <des>Fragment帮助类</des> <value>org.aisen.weibo.sina.ui.fragment.base.FragmentHelper</value> </setting> --> <setting type="activity_helper"> <des>Activity帮助类</des> <value>org.aisen.weibo.sina.ui.activity.basic.AisenActivityHelper</value> </setting> <setting type="weico_key"> <value>211160679</value> </setting> <setting type="weico_screet"> <value>63b64d531b98c2dbff2443816f274dd3</value> </setting> <setting type="weico_callback"> <value>http://oauth.weico.cc</value> </setting> </app-settings>
2、解析成的实体bean,SettingBean,Setting,SettingArray,SettingExtras
class SettingBean implements Serializable { private static final long serialVersionUID = -3694407301270573142L; private String description; private String type; private String value; public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } }
public class Setting extends SettingBean implements Serializable { private static final long serialVersionUID = 4801654811733634325L; private Map<String,SettingExtra> extras; public Setting() { extras = new HashMap<String,SettingExtra>(); } public Map<String,SettingExtra> getExtras() { return extras; } public void setExtras(Map<String,SettingExtra> extras) { this.extras = extras; } }
public class SettingArray extends SettingBean implements Serializable { private static final long serialVersionUID = 6482515166206579219L; private List<Setting> settingArray; private int index; public SettingArray() { settingArray = new ArrayList<Setting>(); } public List<Setting> getSettingArray() { return settingArray; } public void setSettingArray(List<Setting> settingArray) { this.settingArray = settingArray; } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } }
public class SettingExtra extends SettingBean implements Serializable { private static final long serialVersionUID = 4608815242740722900L; }
3、解析的方法,返回的是一个map集合
static Map<String,Setting> parseSettings(Context context,String fileName) { Map<String,Setting> settingMap = new HashMap<String,Setting>(); List<SettingArray> settingArray = new ArrayList<SettingArray>(); List<SettingExtra> settingExtras = null; Setting readSetting = null; SettingArray readSettingArray = null; SettingExtra readSettingExtra = null; XmlPullParser xmlResParser = null; try { String packageName = context.getPackageName(); Resources resources = context.getPackageManager().getResourcesForApplication(packageName); Logger.d("read xml resource,filename = " + fileName); int resId = resources.getIdentifier(fileName,"raw",packageName); // 解析URL配置 xmlResParser = Xml.newPullParser(); xmlResParser.setInput(resources.openRawResource(resId),"utf-8"); int eventType = xmlResParser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_TAG: if ("setting-array".equals(xmlResParser.getName())) { readSettingArray = new SettingArray(); readSettingArray.setType(xmlResParser.getAttributeValue(null,"type")); readSettingArray.setIndex(Integer.parseInt(xmlResParser.getAttributeValue(null,"index"))); } if ("setting".equals(xmlResParser.getName())) { readSetting = new Setting(); readSetting.setType(xmlResParser.getAttributeValue(null,"type")); } if ("extras".equals(xmlResParser.getName())) { settingExtras = new ArrayList<SettingExtra>(); } if ("extra".equals(xmlResParser.getName())) { readSettingExtra = new SettingExtra(); readSettingExtra.setType(xmlResParser.getAttributeValue(null,"type")); } if ("des".equals(xmlResParser.getName())) { if (readSettingExtra != null) { readSettingExtra.setDescription(xmlResParser.nextText()); } else if (readSetting != null) { readSetting.setDescription(xmlResParser.nextText()); } else if (readSettingArray != null) { readSettingArray.setDescription(xmlResParser.nextText()); } } if ("value".equals(xmlResParser.getName())) { if (readSettingExtra != null) { readSettingExtra.setValue(xmlResParser.nextText()); } else if (readSetting != null) { readSetting.setValue(xmlResParser.nextText()); } } break; case XmlPullParser.END_TAG: if ("setting".equals(xmlResParser.getName())) { if (readSetting != null) { if (readSettingArray != null) { readSettingArray.getSettingArray().add(readSetting); } else { settingMap.put(readSetting.getType(),readSetting); } } Logger.d(TAG,String.format("parse new setting --->%s",JSON.toJSONString(readSetting))); readSetting = null; } if ("setting-array".equals(xmlResParser.getName())) { settingArray.add(readSettingArray); Logger.d(TAG,String.format("parse new settingArray --->%s",JSON.toJSONString(readSettingArray))); readSettingArray = null; } if ("extras".equals(xmlResParser.getName())) { if (readSetting != null) for (SettingExtra extra : settingExtras) readSetting.getExtras().put(extra.getType(),extra); settingExtras = null; } if ("extra".equals(xmlResParser.getName())) { settingExtras.add(readSettingExtra); Logger.d(TAG,String.format("parse new settingExtra --->%s",JSON.toJSONString(settingExtras))); readSettingExtra = null; } } eventType = xmlResParser.next(); } } catch (Exception e) { e.printStackTrace(); } finally { // if (xmlResParser != null) // xmlResParser.close(); } for (SettingArray array : settingArray) { if (array.getSettingArray().size() > array.getIndex()) { Setting setting = array.getSettingArray().get(array.getIndex()); setting.setType(array.getType()); settingMap.put(setting.getType(),setting); } } return settingMap; }