草庐IT

android - SharedPreferences Editor commit 花费太多时间

coder 2023-12-02 原文

我尝试使用 PreferenceActivity 进行配置 Activity ...

我找到了一些有效的例子,比如

“WiFi高级配置编辑器”

“Wifi 配置编辑器专业版”

但是我写的代码在 editor.commit() 行等待 10-15 秒... 它一定很简单,但我想不通。

这是简短的代码;

...

SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(v.getContext());
prefs.registerOnSharedPreferenceChangeListener(ClassConfig.this);

    SharedPreferences.Editor editor = prefs.edit();
editor.clear();

editor.putString("key1", value1);
editor.putString("key2", value2);
editor.putBoolean("key3", value3);
    ...
    //i got nearly 35 keys here
    ...
    editor.putString("key33", value33);
editor.putBoolean("key34", value34);
editor.putBoolean("key35", value35);

    editor.commit();

有什么想法吗??

更新:还有一件事。我在日志文件中看到了这些警告

W/BackupManagerService(1914) dataChanged but no participant pkg='com.android.providers.settings' uid=10046

最佳答案

commit() 是同步执行的,所以你注意到它花了很多时间.. 请改用 apply()

https://stackoverflow.com/a/5960743/1233659

关于android - SharedPreferences Editor commit 花费太多时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4895823/

有关android - SharedPreferences Editor commit 花费太多时间的更多相关文章

随机推荐