Rya*_*yan 5 recursion android overflow sharedpreferences
我刚刚发布了我的第一个Android动态壁纸.我在我的手机和几个朋友的手机上测试它没有发现任何问题,但显然在某些设备上它被卡在递归循环中并在用户尝试更改设置时导致堆栈溢出错误.
我相信问题正在发生,因为我有一些需要更改其他几个持久值的"主题"设置.例如,一个主题将设置默认颜色,速度,背景等.似乎当我使用Editor.commit()以编程方式持久保存这些值时,它再次调用onSharedPreferenceChanged,并再次...
由于这是一个动态壁纸,我在透明首选项屏幕后面运行预览,我需要它来反映设置的变化.我还需要滑块/颜色选择器/列表首选项来反映用户直接进行的更改,以及选择"主题"时的编程方式.最简单的方法似乎是使用onSharedPreferenceChanged中的首选项编辑器来更改它们,实际上,这适用于许多设备.
我该怎么做才能使它适用于所有设备?
这是相关的代码:
public void onSharedPreferenceChanged(SharedPreferences prefs, String key)
{
if(key != null)
{
SharedPreferences.Editor editor = prefs.edit();
hue = prefs.getInt("color", 0);
BG_COLOR = prefs.getInt("background_color", 0);
//etc...
if(key.matches("plasma_set"))
{
plasmaAtlasName = atlasName;
editor.putString("atlasName", atlasName);
//load each bolt set with defalut values
if(plasmaAtlasName.equals("plasmaAtlas11"))
{
hue = 180;
editor.putInt("speed", 10);
editor.putInt("bolt_density", 2);
BG_COLOR = 0;
editor.putInt("background_color", BG_COLOR);
editor.putInt("color", hue);
}
if(plasmaAtlasName.equals("plasmaAtlas9"))
{
hue = 330;
editor.putInt("speed", 10);
editor.putInt("bolt_density", 2);
BG_COLOR = 0;
editor.putInt("background_color", BG_COLOR);
editor.putInt("color", hue);
}
//etc...
}
editor.commit();
}
}
Run Code Online (Sandbox Code Playgroud)