oschina-app源码解析-系统参数保存
2014-02-27 14:51
369 查看
最近研究了下oschina-app 的源码,有几个功能实现的挺不错,比如主页面的页面切换效果、开源UIGrendroid的使用、数据的缓存、以及html5的加载、页面动画使用等,在这里跟大家分享一下。
主页面是否能切左右滑动是可以设置的,这种参数我经常是保存在sharepreference保存的,这里是用系统文件对Properties保存的。
通过 Properties对参数数据的保存、获取、删除代码:
以上为通过Properties对app参数的保存和读取,下面提供另一种方法SharedPreferences对参数对象的统一存储和读取,直接上代码
读写过程,先开辟一块内存空间,对数据进行编解码,在对对象就行读写。我建议尽量使用文件来储存参数和临时参数,尽量避免由于系统回收,把静态参数给销毁,导致不必要的异常。
oschina-app完整源码下载:http://download.csdn.net/detail/xiangxue336/7023661
主页面是否能切左右滑动是可以设置的,这种参数我经常是保存在sharepreference保存的,这里是用系统文件对Properties保存的。
通过 Properties对参数数据的保存、获取、删除代码:
public String get(String key) { Properties props = get(); return (props != null) ? props.getProperty(key) : null; } public Properties get() { FileInputStream fis = null; Properties props = new Properties(); try { // 读取files目录下的config // fis = activity.openFileInput(APP_CONFIG); // 读取app_config目录下的config File dirConf = mContext.getDir(APP_CONFIG, Context.MODE_PRIVATE); fis = new FileInputStream(dirConf.getPath() + File.separator + APP_CONFIG); props.load(fis); } catch (Exception e) { } finally { try { fis.close(); } catch (Exception e) { } } return props; } private void setProps(Properties p) { FileOutputStream fos = null; try { // 把config建在files目录下 // fos = activity.openFileOutput(APP_CONFIG, Context.MODE_PRIVATE); // 把config建在(自定义)app_config的目录下 File dirConf = mContext.getDir(APP_CONFIG, Context.MODE_PRIVATE); File conf = new File(dirConf, APP_CONFIG); fos = new FileOutputStream(conf); p.store(fos, null); fos.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { fos.close(); } catch (Exception e) { } } } public void set(Properties ps) { Properties props = get(); props.putAll(ps); setProps(props); } public void set(String key, String value) { Properties props = get(); props.setProperty(key, value); setProps(props); } public void remove(String... key) { Properties props = get(); for (String k : key) props.remove(k); setProps(props); }
以上为通过Properties对app参数的保存和读取,下面提供另一种方法SharedPreferences对参数对象的统一存储和读取,直接上代码
/** * 保存用户信息 * * @throws IOException */ public void saveUser() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos; try { oos = new ObjectOutputStream(baos); oos.writeObject(mUser); String object64 = new String( Base64.encodeBase64(baos.toByteArray())); mPrefere.edit().putString("user", object64).commit(); } catch (IOException e) { logUtil.i(e.toString()); e.printStackTrace(); } } /** * 获取用户信息对象 * * @return * @throws StreamCorruptedException * @throws IOException * @throws ClassNotFoundException */ public StudentInfo getUser() { StudentInfo user = null; try { String object64 = mPrefere.getString("user", ""); byte[] buff = Base64.decodeBase64(object64.getBytes()); ByteArrayInputStream bais = new ByteArrayInputStream(buff); ObjectInputStream ois; ois = new ObjectInputStream(bais); user = (StudentInfo) ois.readObject(); } catch (Exception e) { logUtil.i(e.toString()); // TODO Auto-generated catch block e.printStackTrace(); } return user; }
读写过程,先开辟一块内存空间,对数据进行编解码,在对对象就行读写。我建议尽量使用文件来储存参数和临时参数,尽量避免由于系统回收,把静态参数给销毁,导致不必要的异常。
oschina-app完整源码下载:http://download.csdn.net/detail/xiangxue336/7023661
相关文章推荐
- 通用权限管理系统组件中实现系统参数配置保存,附源码
- 利用App.Config保存系统工作参数
- oschina-app源码解析-webview重组html
- 通用权限管理系统组件 (GPM - General Permissions Manager) 中实现系统参数配置保存,附源码
- oschina-app源码解析-webview处理超链接
- 通用权限管理系统组件 (GPM - General Permissions Manager) 中实现系统参数配置保存,附源码
- C#源码 批量结束进程 让系统加速 参数配置保存
- oschina-app源码分析-提醒标签BadgeView使用逻辑流程
- postgresql源码分析--解析sql--类型系统
- 向Android系统添加自定义的APP源码
- 手机直播APP源码、直播系统开发经验分享
- android源码解析之(十二)-->系统启动并解析Manifest的流程
- C# 读取保存App.config配置文件的完整源码参考
- Android源码解析之存储系统
- iPhone :把app的参数设置整合到系统的设置菜单里
- Activity 状态保存 OnSaveInstanceState参数解析
- C# 读取保存App.config配置文件的完整源码参考
- fabric源码解析4——配置系统
- Spring Boot 实现原理与源码解析系统 —— 精品合集
- 安卓系统源码--DeskClock源码解析(三)枚举