I am looking for a method to store settings persistently on android device, from Kivy framework.
I found Kivy documentation, overall informative, vague in this particular area. It mentions three methods (sorry, dont have enough reputation to provide clicable links, relative paths to kivy.org provided, I'd be glad if someone could fix those links):
- [Storage] ./docs/api-kivy.storage.html#module-kivy.storage
- [Settings] ./docs/api-kivy.uix.settings.html
- [Config] ./docs/api-kivy.config.html
In addition to those, I'm aware that I could store data in a file, via pickle or database, but I'd like to use specifically sharedpreferences
, or at least any Android/Kivy specific persistent storage.
However, I was unable to find any comparison, or explanation how they are different, and how they are used. Could anyone shed some light, had used them already?
Actually, I'm 80% sure that neither of this method uses Android's shared preferences, thus I thought about using jnius (4), and to do that I've tried (methods 1,2/3?,4), based on simple hello world example:
from kivy.app import App from kivy.uix.button import Button import jnius from kivy.config import Config from kivy.storage.dictstore import DictStore class MyApp(App): def build(self): path = "DEFAULT" try: path = Config.get('kivy', 'my_important_variable') print "\t\t\t KIVY 1:", Config.get('kivy', 'my_important_variable') except Exception as err: print ("KIVY, 1, error: {}".format(repr(err))) try: store = DictStore("MY_SETTINGS") path = store.get("my_important_variable") print "\t\t\t KIVY 2:", path except KeyError as err: print ("KIVY, 2, error: {}".format(repr(err))) try: prefs_m = jnius.autoclass('android.preference.PreferenceManager') prefs = prefs_m.getSharedPreferences() path = prefs.getString("my_important_variable", None) print "\t\t\t KIVY 3:", path except jnius.jnius.JavaException as err: print ("KIVY, 3, error: {}".format(repr(err))) btn1 = Button(text=path) btn1.bind(on_press=app.callback) # return btn1 def callback(self, instance): print('The button <%s> is being pressed, SAVING...' % instance.text) try: Config.set('kivy', 'my_important_variable', "my_value_1") except Exception as err: print ("KIVY, 4, error: {}".format(repr(err))) try: store = DictStore("MY_SETTINGS") store.put("MY_SETTINGS", my_important_variable="my_value_2") except Exception as err: print ("KIVY, 5, error: {}".format(repr(err))) try: prefs_c = jnius.autoclass('android.content.SharedPreferences') prefs_m = jnius.autoclass('android.preference.PreferenceManager') prefs = prefs_m.getSharedPreferences() prefs_e = prefs.Editor() prefs_e.putString("my_important_variable", "my_value_3") prefs_e.commit() except Exception as err: print ("KIVY, 6, error: {}".format(repr(err))) try: context = jnius.autoclass('android.content.Context') # do I actually get context or a class here? prefs = context.getPreferences(0).edit(); prefs.putString("my_important_variable", "my_value_4") prefs.commit() except Exception as err: print ("KIVY, 7, error: {}".format(repr(err))) if __name__ == '__main__': app = MyApp() app.run()
and here are logcat's results
... each time app is launched
I/python ( 5973): KIVY, 1, error: No option 'my_important_variable' in section: 'kivy'
I/python ( 5973): KIVY, 2, error: KeyError('my_important_variable',)
I/python ( 5973): KIVY, 3, error: JavaException('Unable to find a None method!',)
... button pressed
I/python ( 5973): The button <DEFAULT> is being pressed, SAVING...
I/python ( 5973): KIVY, 6, error: JavaException('Unable to find a None method!',)
I/python ( 5973): KIVY, 7, error: AttributeError("type object 'android.content.Context' has no attribute 'getPreferences'",)
Notice, that 4, 5 "error msg's" didn't get called, so in theory they should have worked, but second launch I get same errors. I've run out of ideas how to crack it.