SharedPreference Zmiany nie odzwierciedlone w mojej usłudze tapety
Tworzę tapetę na żywo, w której muszę zmienić prędkość pojazdu na scenie, a następnie nacisnąć przycisk powrotu, aby wrócić do usługi tapety. W mojej aktywności preferencji zapisuję zmiany preferencji listy we współdzielonych preferencjach w następujący sposób: -
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
ListPreference listPreference = (ListPreference) findPreference("listPref");
currValue = listPreference.getValue();
Log.e("LiveWallpaperSettings", "currvalue " + currValue);
listPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference arg0, Object arg1) {
SharedPreferences customSharedPreference = getSharedPreferences("Speed", LiveWallpaperSettings.MODE_PRIVATE);
SharedPreferences.Editor editor = customSharedPreference.edit();
editor.putString("Speed",currValue);
editor.commit();
return true;
}
});
Moja usługa tapety jest wykonywana przy użyciu rozszerzenia andengine livewallpaper. Jeśli chcę odzwierciedlić zmiany w preferencjach mojej listy w usłudze, jak to zrobić. To właśnie zrobiłem, ale nie działa.
Mój prefs.xml
<PreferenceCategory
android:title="Settings">
<ListPreference
android:title="Speed"
android:summary="Change the Speed"
android:key="listPref"
android:defaultValue="15"
android:entries="@array/listArray"
android:entryValues="@array/listValues"
/>
</PreferenceCategory>
Moja tablica.xml
<resources>
<string-array name = "listArray">
<item>Slow</item>
<item>Medium</item>
<item>Fast</item>
</string-array>
<string-array name = "listValues">
<item>5</item>
<item>15</item>
<item>30</item>
</string-array>
W mojej usłudze implementuję SharedPreferences.OnSharedPreferenceChangeListener i implementuję następującą metodę
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) {
sharedPreferences = getSharedPreferences("Speed", MODE_PRIVATE);
strSpeedValue = sharedPreferences.getString("Speed", "5");
fltSpeedValue = Integer.parseInt(strSpeedValue);
final AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(0, 0, 0, 10);
autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(fltSpeedValue, new Sprite(0,mCamera.getHeight() - this.mParallaxLayer.getHeight(),this.mParallaxLayer, getVertexBufferObjectManager())));
autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(0f, new Sprite(CAMERA_WIDTH/2 - 30, CAMERA_HEIGHT/2,this.mAutoLayer, getVertexBufferObjectManager())));
mMainScene.setBackground(autoParallaxBackground);
}
@Override
protected void onResume() {
super.onResume();
// Set up a listener whenever a key changes
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onPause() {
super.onPause();
// Unregister the listener whenever a key changes
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
}
Ale wartość, którą zmieniam w mojej preferencji listy, nie zmienia się w mojej usłudze. Czy robię coś źle?