我有一个设置Activity
/ Fragment
,其中包含3个首选项,这些首选项是从首选项XML文件加载的。当我点击其中一个偏好时,它会启动一项新活动来发现蓝牙设备。我希望能够从此活动中选择蓝牙设备并将其作为首选项(MAC地址)保存。问题是我将设备保存在DiscoverBluetooth
活动中并验证它是否存在,但在返回原始设置活动时,首选项仍为空。
DiscoverBluetooth列表活动:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
mEditor = prefs.edit();
String macAddress = mArrayAdapter.getItem(position).split("\n")[1];
if (D) Log.i(TAG, "Saving saved device with MAC address: " + macAddress);
mEditor.putString(SettingsActivity.PrefsFragment.KEY_SAVED_DEVICE, macAddress).commit();
if (D) Log.i(TAG, "saved device is now: " + prefs.getString(SettingsActivity.PrefsFragment.KEY_SAVED_DEVICE, "Invalid MAC"));
finish();
}
设置活动片段:
public static class PrefsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener{
public static final String KEY_END_CHARACTER = "list_endcharacter";
public final static int DEFAULT_END_CHARACTER = R.string.preferences_list_endcharacter_default;
public static final String KEY_DEVICE_UUID = "edittext_deviceuuid";
public final static int DEFAULT_UUID = R.string.preferences_default_deviceuuid;
public static final String KEY_SAVED_DEVICE = "edittext_saved_device";
public static final char[] END_CHARACTER_VALUES = {'\n', '\r'};
//Preference UI references
private EditTextPreference savedDevicePref;
private EditTextPreference deviceUuid;
private ListPreference endCharacter;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//load the preferences from an xml resource
addPreferencesFromResource(R.xml.preferences);
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onResume(){
super.onResume();
deviceUuid = (EditTextPreference) findPreference(KEY_DEVICE_UUID);
savedDevicePref = (EditTextPreference) findPreference(KEY_SAVED_DEVICE);
endCharacter = (ListPreference) findPreference(KEY_END_CHARACTER);
updateEditTextSummary(deviceUuid);
updateListSummary(endCharacter);
if (savedDevicePref != null){
updateEditTextSummary(savedDevicePref);
} else {
if (D) Log.i(TAG, "saved device pref is null");
}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(KEY_DEVICE_UUID)){
updateEditTextSummary(deviceUuid);
} else if (key.equals(KEY_END_CHARACTER)){
updateListSummary(endCharacter);
} else if (key.equals(KEY_SAVED_DEVICE)){
Log.i(TAG,"New saved device: " + sharedPreferences.getString(key, null));
}
}
private void updateListSummary(ListPreference lPreference){
String summary = "";
final CharSequence[] entries = lPreference.getEntries();
if (entries == null || entries.length == 0){
summary = "";
} else{
summary = lPreference.getEntry().toString();
}
lPreference.setSummary(summary);
}
private void updateEditTextSummary(EditTextPreference etPreference){
String text = etPreference.getText();
etPreference.setSummary(text);
}
}
首选项XML
<?xml version="1.0" encoding="utf-8"?>
<PreferenceCategory
android:title="@string/bluetooth_preferences">
<ListPreference
android:key="list_endcharacter"
android:title="@string/preferences_title_endcharacter"
android:entries="@array/preferences_list_endcharacter_strings"
android:entryValues="@array/preferences_list_endcharacter"
android:defaultValue="@string/preferences_list_endcharacter_default"/>
<Preference
android:title="@string/preferences_title_lastdevice"
android:defaultValue="@string/preferences_default_saved_device">
<intent
android:targetClass="com.robert_stuart.bluetoothtest.DiscoverBluetooth"
android:targetPackage="com.robert_stuart.bluetoothtest"/>
</Preference>
<EditTextPreference
android:key="edittext_deviceuuid"
android:title="@string/preferences_title_deviceuuid"
android:dialogTitle="@string/preferences_title_deviceuuid"
android:defaultValue="@string/preferences_default_deviceuuid"/>
</PreferenceCategory>
选择MAC地址后记录输出
05-27 09:36:03.139: I/DiscoverBluetooth(2834): Saving saved device with MAC address: 00:14:03:02:13:49
05-27 09:36:03.139: I/DiscoverBluetooth(2834): saved device is now: 00:14:03:02:13:49
05-27 09:36:03.156: I/SettingsActivity(2834): saved device pref is null
知道这里发生了什么吗?谢谢你的帮助。
答案 0 :(得分:1)
只是想确保在保存数据时在sharedPreference上调用commit()。你能核实一下吗?
答案 1 :(得分:0)
我意识到我从来没有为XML文件中的首选项定义一个键,所以即使我正确地保存了键,片段也指向什么,因此仍然为空。
<Preference
android:title="@string/preferences_title_lastdevice"
android:key="edittext_saved_device"
android:defaultValue="@string/preferences_default_saved_device">
<intent
android:targetClass="com.robert_stuart.bluetoothtest.DiscoverBluetooth"
android:targetPackage="com.robert_stuart.bluetoothtest"/>
</Preference>