前言

  这段时间在研究android平台上的开源项目——StandupTimer,这是由jwood所设计的一个较为简单android应用,用于控制会议时间,类似秒表倒计时。

PreferenceActivity

  PreferenceActivity是android提供的对系统信息和配置进行自动保存的Activity,它通过 SharedPreference 方式将信息保存在XML 文件当中。使用PreferenceActivity不需要我们对SharedPreference进行操作,系统会自动对Activity 的各种View上的改变进行保存(这个真是太赞了!)。   在android项目中添加一个 android xml 文件需要注意的是这次选择的是 Preference。而不是以往的Layout 这个文件是保存在 res /xml 路径下的。

PreferenceScreen xml

  preference下的View是有限的,只有下面几个:
  • CheckBoxPreference:CheckBox选择项,对应的值的ture或flase
  • EditTextPreference:输入编辑框,值为String类型,会弹出对话框供输入。
  • ListPreference: 列表选择,弹出对话框供选择。
  • Preference:只进行文本显示,需要与其他进行组合使用。
  • PreferenceCategory:用于分组。
  • RingtonePreference:系统玲声选择
更多关于 PreferenceScreen的介绍可以查看博客园上的一篇文章: Android之PreferenceActivity
              <?        xml version="1.0" encoding="utf-8"        ?>                 <        PreferenceScreen         xmlns:android        ="http://schemas.android.com/apk/res/android"        >                 <        CheckBoxPreference         android:key        ="sounds"         android:title        ="@string/play_sounds"         android:summary        ="@string/play_sounds_summary"         android:defaultValue        ="true"        ></        CheckBoxPreference        >                 <        EditTextPreference         android:key        ="warning_time"         android:title        ="@string/warning_time"         android:summary        ="@string/warning_time_summary"         android:defaultValue        ="15"         android:inputType        ="phone"         android:digits        ="0123456789"        ></        EditTextPreference        >                 <        CheckBoxPreference         android:key        ="unlimited_participants"         android:title        ="@string/unlimited_participants"         android:summary        ="@string/unlimited_participants_summary"         android:defaultValue        ="false"        ></        CheckBoxPreference        >                 <        CheckBoxPreference         android:key        ="variable_meeting_length"         android:title        ="@string/variable_meeting_length"         android:summary        ="@string/variable_meeting_length_summary"         android:defaultValue        ="false"        ></        CheckBoxPreference        >                 </        PreferenceScreen        >               

android:key 唯一标识符。它对应保存的XML保存的配置文件中的节点的 name属性 android:defaultValue 默认值,对应XML中的Value属性的值。
              /** * PreferenceActivity 会自动保存更改 */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.addPreferencesFromResource(R.xml.setting); }      

当PreferenceActivity上的View有所更改时,系统会自动将对应的值更新到XML配置文件中,该文件可以在android 的 file explorer 中的 data/data/"yourPageName"/shared_prefs/"yourpageName"_prefenrences.xml中找 到。“yourpageName”表示你项目所在的包。

获取配置信息

  为了方便的获取配置信息,我们可以在PreferenceActivity里添加一些pulbic 方法来公开配置信息的访问。
                 /**       * 获取是否播放声音       */             public             static             boolean       playSounds(Context context) {       return       PreferenceManager.getDefaultSharedPreferences(context).getBoolean(SOUNDS, SOUNDS_DEFAULT); }       /**       * 设置播放的声音 *       @param       context 上下文 *       @param       value 是否播放       */             public             static             void       setPlaySounds(Context context,       boolean       value) { PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(SOUNDS, value).commit(); }       /**       * 获取警告时间 *       @param       context 上下文 *       @return       警告时间秒       */             public             static             int       getWarningTime(Context context) { String value      =      PreferenceManager.getDefaultSharedPreferences(context).getString(WARNING_TIME,Integer.toString(WARNING_TIME_DEFAULT));       try       {       return       Integer.parseInt(value); }       catch      (NumberFormatException e) { setWarningTime(context, WARNING_TIME_DEFAULT);       return       WARNING_TIME_DEFAULT; } }       /**       * 设置警告时间 *       @param       context 上下文 *       @param       warningTime 警告时间       */             public             static             void       setWarningTime(Context context,       int       warningTime) { PreferenceManager.getDefaultSharedPreferences(context).edit().putString(WARNING_TIME, Integer.toString(warningTime)).commit(); }       /**       * 参加人数无限制 *       @param       context *       @return             */             public             static             boolean       allowUnlimitedParticipants(Context context) {       return       PreferenceManager.getDefaultSharedPreferences(context).getBoolean(UNLIMITED_PARTICIPANTS, UNLIMITED_PARTICIPANTS_DEFAULT); }       public             static             void       setAllowUnlimitedParticipants(Context context,       boolean       value) { PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(UNLIMITED_PARTICIPANTS, value).commit(); }       /**       * 允许编辑会议时间 *       @param       context *       @return             */             public             static             boolean       allowVariableMeetingLength(Context context) {       return       PreferenceManager.getDefaultSharedPreferences(context).getBoolean(VARIABLE_MEETING_LENGTH, VARIABLE_MEETING_LENGTH_DEFAULT); }       public             static             void       setAllowVariableMeetingLength(Context context,       boolean       value) { PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(VARIABLE_MEETING_LENGTH, value).commit(); }     

  getDefaultSharedPreferences(Context )用来获取preferences.以后的操作就和普通的Sharedpreferences一样了,如果需要修改某项配置的信息,记得最后需要 commit()。

当其他地方需要使用配置时,可以直接调用 setting.getXXX() 方法来获取配置信息。

更多相关文章

  1. 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
  2. Android中的几种网络请求方式详解
  3. android 流量管理
  4. android SystemClock设置系统时间需要system权限
  5. Android(安卓)Wi-Fi基本操作
  6. Android:自定义控件你应该知道的这些事_TypedArray
  7. android文件系统挂载分析(1)---正常开机挂载,分区信息解读
  8. android开发,你还在犹豫什么呢?进来看看花不了你很多时间的
  9. Android(安卓)文件打开方式

随机推荐

  1. Android(安卓)创建与解析XML(一)—— 概述
  2. android获取设备唯一标识完美解决方案
  3. android介绍以及学习方法
  4. android 深入需要学习的
  5. 详细的北京安卓培训班课程内容介绍
  6. Android(安卓)am 指令的使用
  7. 用phoneGap打包一个基于Android的WebApp
  8. 使用GDB调试Android(安卓)NDK native(C/C
  9. Android搭建HTTP和WebSocket的服务器端
  10. Android的消息机制————读书笔记