本文介绍了Kivy和android sharedpreferences的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am looking for a method to store settings persistently on android device, from Kivy framework.

我发现Kivy文档在这个特定领域中内容全面,含糊不清.它提到了三种方法(抱歉,没有足够的声誉提供可靠的链接,提供了 kivy.org 的相对路径,很高兴有人可以修复这些链接 ):

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):

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

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

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.

  1. 但是,我找不到任何比较,也无法解释它们的不同之处以及如何使用它们.有人可以使用它吗?

  1. 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?

实际上,我有80%的把握确保此方法均未使用Android的共享首选项,因此我考虑过使用jnius(4),并尝试这样做(方法1,2/3? ,4),基于简单的hello world示例:

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()

这是logcat的结果

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'",)

注意,没有调用4、5个错误消息",因此从理论上讲它们应该起作用,但是第二次启动时我遇到了相同的错误.我已经没有办法破解它了.

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.

推荐答案

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

Kivy.Config is used to store settings that relate to the instantiation of the App class.It is usually placed at the very top of your Python script before any other kivy module is imported.This method is not platform specific, but the default path to the config file changes depending on the platform.

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

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

The DictStore is a storage class that stores a dictionary to disk. The filename argument specifies the name of the file where the dictionary is stored.When the get function is called, a Python dictionary is returned.

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/

I will provide the code for accessing shared prefs below.If you are interested in learning more please read http://developer.android.com/guide/topics/data/data-storage.html and 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()

这篇关于Kivy和android sharedpreferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 07:02
查看更多