我正在制作一个动态壁纸,在设置场景中需要更改车辆的速度,并在按下返回按钮时将其反映回壁纸服务。在我的首选活动中,我将列表首选项更改保存在共享首选项中:
@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;
}
});
我的壁纸服务是使用AndEngine LiveWallpaper扩展程序制作的。如果我想在服务中反映出列表首选项的更改,我该怎么做?这是我的尝试,但似乎不起作用。
我的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>
我的array.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>
在我的服务中,我使用SharedPreferences.OnSharedPreferenceChangeListener并实现了以下方法:
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);
}
但是我在ListPreference中更改的值并没有在我的服务中得到更改。我做错了什么吗?