Getting API settings that works on both Android and PC

I would like to save user preferences from a sketch working either on a PC or on an Android phone using the same code as the standard "Java way".

The ideal candidate for my purposes is the java.util.prefs.Preferences class. So, I wrote a small test script to see if it works in processing:

String prId = "counter";
Preferences prefs = Preferences.userNodeForPackage(this.getClass());
int counter = prefs.getInt(prId, 0);

println(counter);
prefs.putInt(prId, 1+counter);

This program displays an increasing number of times each time it is executed - on a PC. On Android, the default value (0) is always displayed. Are there any additional steps required to work on Android? Permissions for the request? Are there alternatives for storing name-value pairs on both platforms?

Designation: WRITE_EXTERNAL_STORAGE permission already enabled

+5
2

, Android: https://code.google.com/p/android/issues/detail?id=22232

, , , .

(-) java.util.Properties, .

( - API ) AbstractPreferences (, Android SharedPreferences?), . SO: java.util.Preferences Windows , ?

P.S.. / Preferences.exportSubtree(OutputStream os) Preferences.importPreferences(InputStream is)

+2

Android SharedPreferences. :

String prId = "counter";
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
int counter = prefs.getInt(prId,0);    // Get int, default is 0

SharedPreferences.Editor prefsEditor = prefs.edit();
prefsEditor.putInt(prId, counter++);    // Put counter back
prefsEditor.commit();    //Don't forget to commit the changes

, java.util.prefs.Preferences Android. , , Activity .

, , . , /.java/.userPrefs/**yourpacakgename**/prefs.xml Android, Windows ~/Library/Preferences/**yourpacakgename**/prefs.xml Mac OS X. , Preferences.userRoot() Android, root.

0

All Articles