Skip to main content
 首页 » 编程设计

python之Kivy 和 android 共享首选项

2025年05月04日1298°冷暖

我正在寻找一种从 Kivy 框架中将设置持久存储在 Android 设备上的方法。

我发现 Kivy 文档总体上提供了丰富的信息,但在这个特定领域含糊不清。它提到了三种方法(抱歉,没有足够的声誉来提供可点击的链接,提供了到 kivy.org 的相对路径,如果有人可以修复这些链接我会很高兴):

  1. [存储] ./docs/api-kivy.storage.html#module-kivy.storage
  2. [设置] ./docs/api-kivy.uix.settings.html
  3. [配置] ./docs/api-kivy.config.html

除此之外,我知道我可以通过 pickle 或数据库将数据存储在文件中,但我想专门使用 sharedpreferences ,或者至少是任何 Android/Kivy 特定的持久存储。

  1. 但是,我找不到任何比较或解释它们的不同之处以及它们的使用方式。任何人都可以阐明一些已经使用过的信息吗?

  2. 实际上,我有 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 个“错误消息”,因此理论上它们应该有效,但第二次启动时我遇到了同样的错误。 我已经想不出如何破解它了。

请您参考如下方法:

Kivy.Config 用于存储与 App 类实例化相关的设置。 在导入任何其他 kivy 模块之前,它通常位于 Python 脚本的最顶部。 此方法不特定于平台,但配置文件的默认路径会根据平台而变化。

from kivy.config import Config 
desktop=Config.getint('kivy', 'desktop') 
if desktop == 1: 
    print "This app is being run on a desktop." 

DictStore 是一个将字典存储到磁盘的存储类。文件名参数指定存储字典的文件的名称。 当调用 get 函数时,返回一个 Python 字典。

from kivy.app import App 
from kivy.uix.button import Button 
from kivy.storage.dictstore import DictStore 
 
class TestApp(App): 
    def build(self): 
        try: 
            store = DictStore(filename="MY_SETTINGS") 
            dictionary = store.get("my_important_variable") 
            print "\t\t\t KIVY 2: DictStore Succeeded", 
        except KeyError as err: 
            dictionary = {'name': 'None'} 
            print ("KIVY, 2, error: {}".format(repr(err))) 
 
        self.text = str(dictionary) 
        btn1 = Button(text=self.text) 
        btn1.bind(on_press=self.callback) # 
        return btn1 
 
    def callback(self, instance): 
        print('The button <%s> is being pressed, SAVING...' % instance.text) 
        try: 
            store = DictStore(filename="MY_SETTINGS") 
            store.put("my_important_variable", name="John") 
        except Exception as err: 
            print ("KIVY, 5, error: {}".format(repr(err))) 
 
 
 
if __name__ == '__main__': 
    TestApp().run() 

我将在下面提供访问共享首选项的代码。 如果您有兴趣了解更多信息,请阅读 http://developer.android.com/guide/topics/data/data-storage.html和 https://kivy.org/planet/2015/04/python-on%C2%A0android/

from kivy.app import App 
from kivy.uix.button import Button 
 
import jnius 
 
class TestApp(App): 
    def build(self): 
        try: 
            PythonActivity = jnius.autoclass('org.renpy.android.PythonActivity') 
            activity = PythonActivity.mActivity 
            cntxt = activity.getApplicationContext() 
            prefs = cntxt.getSharedPreferences("MY_PREFS", cntxt.MODE_PRIVATE ) 
            print "KIVY ACQUIRED SHARED PREFS" 
            myVar = prefs.getString("my_important_variable", "Default String") 
            print "\tKIVY 3: Retrieved SharedPref" 
        except jnius.jnius.JavaException as err: 
            myVar="Error Loading Prefs." 
            print ("KIVY, 3, error: {}".format(repr(err))) 
 
        self.text = myVar 
        btn1 = Button(text=self.text) 
        btn1.bind(on_press=self.callback) # 
        return btn1 
 
    def callback(self, instance): 
        print('The button <%s> is being pressed, SAVING...' % instance.text) 
        try: 
            PythonActivity = jnius.autoclass('org.renpy.android.PythonActivity') 
            activity = PythonActivity.mActivity 
            cntxt = activity.getApplicationContext() 
            prefs = cntxt.getSharedPreferences("MY_PREFS", cntxt.MODE_PRIVATE) 
            editor = prefs.edit() 
            editor.putString("my_important_variable", "This is important!") 
            editor.commit() 
            print "\tKIVY: Added string <This is important!> to shared prefs." 
        except Exception as err: 
            print ("\tKIVY, 6, error: {}".format(repr(err))) 
 
if __name__ == '__main__': 
    TestApp().run()