我正在使用 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);
}
}