SharedPreferences 编辑器提交需要很长时间
我尝试使用PreferenceActivity进行配置活动...
我发现了一些工作示例,例如
“WiFi高级配置编辑器”
和
“Wifi配置编辑器专业版”,
但我编写的代码在上等待10-15秒editor.commit() 行
... 它一定很简单,但我不明白。
这是简短的代码;
...
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) 数据已更改但没有参与者 pkg='com.android.providers.settings' uid=10046
I try to make a configuration activity using PreferenceActivity...
I found some working examples like
"WiFi Advanced Configuration Editor"
and
"Wifi Config Editor Pro"
but the code I wrote waits for 10-15 seconds on the line editor.commit()
...
it must be very simple but I cant figure out.
here is the brief code;
...
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();
Any ideas??
Update: one more thing. I saw these warnings in the log file
W/BackupManagerService(1914)
dataChanged but no participant
pkg='com.android.providers.settings'
uid=10046
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
commit()
是同步执行的,所以你注意到它花费了很多时间..请改用
apply()
。https://stackoverflow.com/a/5960743/1233659
commit()
is executed synchronously, so you notice that it takes so much time..Use
apply()
instead.https://stackoverflow.com/a/5960743/1233659
提交大的首选项很慢 - 它应该在单独的线程中完成。考虑在
AsyncTask
中实现它Committing large preferences is slow - it should be done in separate thread. Consider implementing this in
AsyncTask
您应该使用异步的
apply()
方法。请参阅此处的文档You should use
apply()
method which is asynchronous. See docs here你在哪里做这个?关于共享首选项更改?
如果您使用的是 PreferenceActivity,则不需要手动编写首选项,因为用户更改小部件状态应该更改 XML 中为 PreferenceActivity 定义的键。
Where are you doing this? OnSharedPreferenceChanged?
If you're using a PreferenceActivity, you shouldn't need to manually write your prefs, as changing widget state by the user should change the key defined in XML for the PreferenceActivity.