gpt4 book ai didi

android - SharedPreference 更改未反射(reflect)在我的墙纸服务中

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:17:29 27 4
gpt4 key购买 nike

我正在制作一个动态壁纸,我需要在设置场景中更改车辆的速度,并且需要在我按下返回按钮时将其反射(reflect)回壁纸服务。在我的偏好 Activity 中,我将列表偏好更改保存在共享偏好中,如下所示:-

@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 动态壁纸扩展制作的。如果我想在服务中反射(reflect)我的列表偏好的变化,我该怎么做。这就是我所做的,但它似乎没有用。

我的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>

我的数组.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);
}

但是我在我的列表首选项中更改的值在我的服务中没有改变。我做错了什么吗?

最佳答案

解决了!

我在 PreferenceActivity 中设置的值不正确,而且我没有正确实现 OnSharedPreferenceChangeListener。

解决方案:-

 ListPreference listPreference;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
listPreference = (ListPreference) findPreference("listPref");

}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

SharedPreferences customSharedPreference = getSharedPreferences(key, LiveWallpaperSettings.MODE_PRIVATE);
SharedPreferences.Editor editor = customSharedPreference.edit();
editor.putString("Speed",listPreference.getValue());
editor.commit();
Log.e("LiveWallpaperSettings", "Speed Value after setting " + customSharedPreference.getString("Speed", ""));
}



@Override
protected void onResume() {
super.onResume();
// Set up a listener whenever a key changes
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}

@Override
protected void onPause() {
super.onPause();
// Unregister the listener whenever a key changes
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}

关于android - SharedPreference 更改未反射(reflect)在我的墙纸服务中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12889753/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com