我正在使用andengine制作动态壁纸。问题出在设置屏幕上。当我按下预览屏幕中的设置按钮时,我的onSharedPreferenceChanged()方法被自动调用..我认为除非我改变一些偏好,否则不应该这样做。以下是我的实施。有人能告诉我,我在做什么错事?
public class LiveWallpaperSettings extends PreferenceActivity implements OnSharedPreferenceChangeListener{
ListPreference listPreferencespeed, listPreferenceConst, listPreferenceCld;
public static boolean mSettingsChanged = false;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
listPreferencespeed = (ListPreference) findPreference("listPref");
listPreferenceConst= (ListPreference) findPreference("listPrefConst");
listPreferenceCld = (ListPreference) findPreference("listPrefCld");
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Log.e("LiveWallpaperSettings", "Key :- " + key);
SharedPreferences customSharedPreference = getSharedPreferences(key, LiveWallpaperSettings.MODE_PRIVATE);
SharedPreferences.Editor editor = customSharedPreference.edit();
editor.putString("Speed",listPreferencespeed.getValue());
editor.putString("Cld", listPreferenceCld.getValue());
editor.putString("Const", listPreferenceConst.getValue());
editor.commit();
mSettingsChanged = true;
Log.e("LiveWallpaperSettings", "Speed Value after setting " + customSharedPreference.getString("Speed", ""));
Log.e("LiveWallpaperSettings", "Cloud Value after setting " + customSharedPreference.getString("Cld", ""));
Log.e("LiveWallpaperSettings", "Constellations Value after setting " + customSharedPreference.getString("Constd", ""));
Log.e("LiveWallpaperSettings", "mSettingsChanged " + mSettingsChanged);
}
@SuppressWarnings("deprecation")
@Override
protected void onResume() {
super.onResume();
// Set up a listener whenever a key changes
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@SuppressWarnings("deprecation")
@Override
protected void onPause() {
super.onPause();
// Unregister the listener whenever a key changes
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
}
答案 0 :(得分:0)
问题可能不在您发布的代码中,而是在设置侦听器的代码中。在代码中查找类似的内容:
sharedPreferences.registerOnSharedPreferenceChangeListener(本);
并检查它是否正确放置。例如,如果设置了侦听器,然后初始化值,则可能需要反转该顺序。