android的PreferenceActivity

前言

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

PreferenceActivity

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