6

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

ppreetikaa
  • 1,025
  • 2
  • 14
  • 22
caw
  • 76
  • 1
  • 4

4 Answers4

3

commit() is executed synchronously, so you notice that it takes so much time.. Use apply() instead.

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

Community
  • 1
  • 1
Hamzeh Soboh
  • 7,070
  • 5
  • 37
  • 54
2

You should use apply() method which is asynchronous. See docs here

2

Committing large preferences is slow - it should be done in separate thread. Consider implementing this in AsyncTask

pixel
  • 21,352
  • 30
  • 113
  • 196
  • check this out please.. [link](http://code.google.com/p/android-wifi-ace/source/browse/trunk/WiFiACE/src/org/marcus905/wifi/ace/WiFiACEList.java?r=11) no threading but also no waiting... i cant get the point... – caw Feb 07 '11 at 15:07
  • I don't know if this application runs fast enough. For explanation checkout this question (especially 4-th bullet in Brad Fitzpatrick answer): [should-accessing-sharepreferences-be-done-off-the-ui-thread](http://stackoverflow.com/questions/4371273/should-accessing-sharepreferences-be-done-off-the-ui-thread) – pixel Feb 07 '11 at 15:17
  • i will try futuretask at a convenient time. – caw Feb 08 '11 at 05:51
0

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.

  • im doing this before using PreferenceActivity on another activity's button onclicklistener. – caw Feb 07 '11 at 15:13