onSharedPreferenceChanged 在某些 Android 设备上导致堆栈溢出

发布于 2025-01-07 03:00:43 字数 1542 浏览 4 评论 0原文

我刚刚发布了我的第一个 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();
}
}

I've just released my first live wallpaper for android. I bug tested it on my phone and several friends phones without finding any problems, but apparently on some devices it's getting stuck in a recursive loop and causing a stack overflow error when the user tries to change settings.

I believe the problem is occurring because I have certain "theme" settings which need to change several other persisted values. For example one theme will set a default color, speed, background, etc. It seems that when I persist these values programmatically with Editor.commit(), it's calling onSharedPreferenceChanged again, and again, and again...

Since this is a live wallpaper, I have a preview running behind the transparent preference screen, and I need it to reflect the settings changes as they're made. I also need the sliders/color pickers/list preferences to reflect changes made both by the user directly, and programmatically when a "theme" is selected. The easiest way to do this seemed to be to change them with a preference editor in onSharedPreferenceChanged, and indeed, this works on many devices.

What can I do to make it work on all devices?

Here's the relevant code:

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();
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流年里的时光 2025-01-14 03:00:43

好吧,我明白了。在调用 Editor.commit() 之前取消注册侦听器,然后再次注册它是一个简单的问题。

Ok, I figured it out. It was a simple matter of unregistering the listener before calling Editor.commit() and then registering it again afterward.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文