public class PreferenceActivityMain extends PreferenceActivity implements OnSharedPreferenceChangeListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置共享引用名
getPreferenceManager().setSharedPreferencesName(AndroidPreferencesActivity.PREFERENCENAME);
//设置共享引用资源文件
addPreferencesFromResource(R.xml.preferences);
//注册监听
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
Log.i("PreferenceActivityMain", key);
Toast.makeText(PreferenceActivityMain.this, sharedPreferences.getString(key, "error").toString(), Toast.LENGTH_SHORT).show();
}
}
共享引用资源文件:preferences.xml
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/inline_preferences">
<CheckBoxPreference
android:key="checkbox_preference"
android:title="@string/title_checkbox_preference"
android:summary="@string/summary_checkbox_preference" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/dialog_based_preferences">
<EditTextPreference
android:key="edittext_preference"
android:title="@string/title_edittext_preference"
android:summary="@string/summary_edittext_preference"
android:dialogTitle="@string/dialog_title_edittext_preference" />
<ListPreference
android:key="list_preference"
android:title="@string/title_list_preference"
android:summary="@string/summary_list_preference"
android:entries="@array/entries_list_preference"
android:entryValues="@array/entryvalues_list_preference"
android:dialogTitle="@string/dialog_title_list_preference" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/launch_preferences">
<!-- This PreferenceScreen tag serves as a screen break (similar to page break
in word processing). Like for other preference types, we assign a key
here so it is able to save and restore its instance state. -->
<PreferenceScreen
android:key="screen_preference"
android:title="@string/title_screen_preference"
android:summary="@string/summary_screen_preference">
<!-- You can place more preferences here that will be shown on the next screen. -->
<CheckBoxPreference
android:key="next_screen_checkbox_preference"
android:title="@string/title_next_screen_toggle_preference"
android:summary="@string/summary_next_screen_toggle_preference" />
</PreferenceScreen>
<PreferenceScreen
android:title="@string/title_intent_preference"
android:summary="@string/summary_intent_preference">
<intent android:action="android.intent.action.VIEW"
android:data="http://www.android.com" />
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory
android:title="@string/preference_attributes">
<CheckBoxPreference
android:key="parent_checkbox_preference"
android:title="@string/title_parent_preference"
android:summary="@string/summary_parent_preference" />
<!-- The visual style of a child is defined by this styled theme attribute. -->
<CheckBoxPreference
android:key="child_checkbox_preference"
android:dependency="parent_checkbox_preference"
android:layout="?android:attr/preferenceLayoutChild"
android:title="@string/title_child_preference"
android:summary="@string/summary_child_preference" />
</PreferenceCategory>
</PreferenceScreen>
strings 资源文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, AndroidPreferencesActivity!</string>
<string name="app_name">AndroidPreferences</string>
<string name="inline_preferences">PreferenceCategory</string>
<string name="title_checkbox_preference">title_checkbox_preference</string>
<string name="summary_checkbox_preference">summary_checkbox_preference</string>
<string name="title_switch_preference">title_switch_preference</string>
<string name="summary_switch_preference">summary_switch_preference</string>
<string name="summary_switch_preference_yes_no">summary_switch_preference_yes_no</string>
<string name="dialog_based_preferences">dialog_based_preferences</string>
<string name="title_edittext_preference">title_edittext_preference</string>
<string name="summary_edittext_preference">summary_edittext_preference</string>
<string name="dialog_title_edittext_preference">dialog_title_edittext_preference</string>
<string name="title_list_preference">title_list_preference</string>
<string name="summary_list_preference"></string>
<string name="dialog_title_list_preference">dialog_title_list_preference</string>
<string name="launch_preferences">launch_preferences</string>
<string name="title_screen_preference">title_screen_preference</string>
<string name="summary_screen_preference">summary_screen_preference</string>
<string name="title_next_screen_toggle_preference">title_next_screen_toggle_preference</string>
<string name="summary_next_screen_toggle_preference">summary_next_screen_toggle_preference</string>
<string name="title_intent_preference">title_intent_preference</string>
<string name="summary_intent_preference">summary_intent_preference</string>
<string name="preference_attributes">preference_attributes</string>
<string name="title_parent_preference">title_parent_preference</string>
<string name="summary_parent_preference">summary_parent_preference</string>
<string name="title_child_preference">title_child_preference</string>
<string name="summary_child_preference">summary_child_preference</string>
<string-array name="entries_list_preference">
<item>Hello</item>
<item>Hi</item>
<item>Bye</item>
</string-array>
<string-array name="entryvalues_list_preference">
<item>Hello</item>
<item>Hi</item>
<item>Bye</item>
</string-array>
</resources>
在另外的Activity读取上面共享引用数据:
SharedPreferences preference = getSharedPreferences(PREFERENCENAME,MODE_WORLD_READABLE);
boolean checkbox_preference = preference.getBoolean("checkbox_preference", false);
String list_preference = preference.getString("list_preference", "none");
String text = checkbox_preference + "---------" + list_preference + "----------" + preference.contains("checkbox_preference");
Toast.makeText(AndroidPreferencesActivity.this, text, Toast.LENGTH_SHORT).show();