我正在寻找一种从 Kivy 框架将设置持久存储在 android 设备上的方法。
我发现 Kivy 文档,整体信息丰富,在这个特定领域含糊不清。它提到了三种方法(抱歉,没有足够的声誉来提供 clicable 链接,提供到kivy.org的相对路径,如果有人可以修复这些链接,我会很高兴):
- [存储] ./docs/api-kivy.storage.html#module-kivy.storage
- [设置] ./docs/api-kivy.uix.settings.html
- [配置] ./docs/api-kivy.config.html
除此之外,我知道我可以通过 pickle 或数据库将数据存储在文件中,但我想特别使用sharedpreferences
,或者至少使用任何 Android/Kivy 特定的持久存储。
但是,我找不到任何比较或解释它们有何不同以及如何使用它们。任何人都可以阐明一下,已经使用过它们了吗?
实际上,我 80% 确定这种方法都没有使用 Android 的共享首选项,因此我考虑使用 jnius (4),并且我已经尝试过(方法 1,2/3?,4),基于简单的 hello world 示例:
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()
这是logcat的结果
... 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'",)
请注意,没有调用 4、5 个“错误消息”,所以理论上它们应该可以工作,但是第二次启动我得到了同样的错误。我已经没有办法破解它了。