<i id='W8iiR'><tr id='W8iiR'><dt id='W8iiR'><q id='W8iiR'><span id='W8iiR'><b id='W8iiR'><form id='W8iiR'><ins id='W8iiR'></ins><ul id='W8iiR'></ul><sub id='W8iiR'></sub></form><legend id='W8iiR'></legend><bdo id='W8iiR'><pre id='W8iiR'><center id='W8iiR'></center></pre></bdo></b><th id='W8iiR'></th></span></q></dt></tr></i><div id='W8iiR'><tfoot id='W8iiR'></tfoot><dl id='W8iiR'><fieldset id='W8iiR'></fieldset></dl></div>

    1. <legend id='W8iiR'><style id='W8iiR'><dir id='W8iiR'><q id='W8iiR'></q></dir></style></legend>
    2. <tfoot id='W8iiR'></tfoot>
    3. <small id='W8iiR'></small><noframes id='W8iiR'>

      • <bdo id='W8iiR'></bdo><ul id='W8iiR'></ul>

      如何使用 kivy 处理 android 运行时权限

      时间:2023-09-01

          <bdo id='TMkGx'></bdo><ul id='TMkGx'></ul>

          <small id='TMkGx'></small><noframes id='TMkGx'>

          <legend id='TMkGx'><style id='TMkGx'><dir id='TMkGx'><q id='TMkGx'></q></dir></style></legend>
            <tfoot id='TMkGx'></tfoot>
                  <tbody id='TMkGx'></tbody>
                <i id='TMkGx'><tr id='TMkGx'><dt id='TMkGx'><q id='TMkGx'><span id='TMkGx'><b id='TMkGx'><form id='TMkGx'><ins id='TMkGx'></ins><ul id='TMkGx'></ul><sub id='TMkGx'></sub></form><legend id='TMkGx'></legend><bdo id='TMkGx'><pre id='TMkGx'><center id='TMkGx'></center></pre></bdo></b><th id='TMkGx'></th></span></q></dt></tr></i><div id='TMkGx'><tfoot id='TMkGx'></tfoot><dl id='TMkGx'><fieldset id='TMkGx'></fieldset></dl></div>

              • 本文介绍了如何使用 kivy 处理 android 运行时权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我发现 kivy 是一个非常好的构建跨平台应用程序的框架,我对 kivy 非常感兴趣,只是为了做 android 应用程序,因为我认为 kivy 很容易和舒适.

                I found that kivy is very nice framework to build cross platform application and I am very interested in kivy just to do android application as I think is easy and comfortable in kivy.

                在尝试了几个例子之后,我很想知道应该如何处理 kivy 应用程序的 android 运行时权限.

                After trying few examples, I am interested to know how should handle android run time permission for the kivy app.

                实际上我在谷歌上搜索过,但没有一个可行的例子.我应该回到 android/java 还是使用 kivy 和其他一些 python 库.

                Actually I had searched on google, but no single working example out there. Should I go back to android / java or it possible with kivy and some other python libs.

                推荐答案

                pyjnius 是要走的路.您必须使用 pyjnius 移植 这些说明.这涉及以下步骤:

                pyjnius is the way to go. You have to port these instructions using pyjnius. This involves the following steps:

                • 不幸的是,对 ContextCompat.checkSelfPermission 的 api 调用是在必须单独下载的 android sdk 支持库中实现的,因此,获取与您的 android API 级别最匹配的版本的 .aar 例如这里.
                • 将其复制到您的项目目录中并从您的 buildozer.spec 中引用它:

                • Unfortunately the api call to ContextCompat.checkSelfPermission is implemented in the android sdk support library which has to be downloaded seperately, so get the .aar with the version best matching your android API level for example here.
                • copy it into your project dir and reference it from your buildozer.spec:

                android.add_aars = support-v4-26.0.0-alpha1.aar  
                

              • 确保 jinius 符合 buildozer.spec 中的要求

              • make sure jinius is in the requirements in buildozer.spec

                使用以下代码片段

                注意:这是一个阻塞功能,它会等待权限对话框得到答复.如果应用程序已经拥有权限,该函数会立即返回.因此,例如,如果您想获得写入 SD 卡和相机的权限,这都是危险权限",请调用:

                Note: this is a blocking function which waits until the permissions dialog is answered. If the app already has the permission the function returns immediately. So for example if you want to get the permissions for writing to the SD card and for the camera, which are both "dangerous permissions", call:

                perms = ["android.permission.READ_EXTERNAL_STORAGE",
                         "android.permission.WRITE_EXTERNAL_STORAGE",
                         "android.permission.CAMERA"]
                
                haveperms = acquire_permissions(perms)
                

                这里是获取权限的函数:

                And here the function for acquiring the permissions:

                import time
                import functools
                import jnius
                
                def acquire_permissions(permissions, timeout=30):
                    """
                    blocking function for acquiring storage permission
                
                    :param permissions: list of permission strings , e.g. ["android.permission.READ_EXTERNAL_STORAGE",]
                    :param timeout: timeout in seconds
                    :return: True if all permissions are granted
                    """
                
                    PythonActivity = jnius.autoclass('org.kivy.android.PythonActivity')
                    Compat = jnius.autoclass('android.support.v4.content.ContextCompat')
                    currentActivity = jnius.cast('android.app.Activity', PythonActivity.mActivity)
                
                    checkperm = functools.partial(Compat.checkSelfPermission, currentActivity)
                
                    def allgranted(permissions):
                        """
                        helper function checks permissions
                        :param permissions: list of permission strings
                        :return: True if all permissions are granted otherwise False
                        """
                        return reduce(lambda a, b: a and b,
                                    [True if p == 0 else False for p in map(checkperm, permissions)]
                                    )
                
                    haveperms = allgranted(permissions)
                    if haveperms:
                        # we have the permission and are ready
                        return True
                
                    # invoke the permissions dialog
                    currentActivity.requestPermissions(permissions, 0)
                
                    # now poll for the permission (UGLY but we cant use android Activity's onRequestPermissionsResult)
                    t0 = time.time()
                    while time.time() - t0 < timeout and not haveperms:
                        # in the poll loop we could add a short sleep for performance issues?
                        haveperms = allgranted(permissions)
                
                    return haveperms
                

                可能最干净的方法是拉皮条 p4a 的 PythonActivity.java 来做到这一点,但现在这个是为我做的.

                Probably the cleanest way would be to pimp p4a's PythonActivity.java to do that but this one does it for me for now.

                这篇关于如何使用 kivy 处理 android 运行时权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

              • 上一篇:Android使用buildozer编译错误 下一篇:如何在Android中保持kivy服务在后台运行(切换到其

                相关文章

                最新文章

              • <i id='6Mqte'><tr id='6Mqte'><dt id='6Mqte'><q id='6Mqte'><span id='6Mqte'><b id='6Mqte'><form id='6Mqte'><ins id='6Mqte'></ins><ul id='6Mqte'></ul><sub id='6Mqte'></sub></form><legend id='6Mqte'></legend><bdo id='6Mqte'><pre id='6Mqte'><center id='6Mqte'></center></pre></bdo></b><th id='6Mqte'></th></span></q></dt></tr></i><div id='6Mqte'><tfoot id='6Mqte'></tfoot><dl id='6Mqte'><fieldset id='6Mqte'></fieldset></dl></div>
                <legend id='6Mqte'><style id='6Mqte'><dir id='6Mqte'><q id='6Mqte'></q></dir></style></legend>

                  <small id='6Mqte'></small><noframes id='6Mqte'>

                    • <bdo id='6Mqte'></bdo><ul id='6Mqte'></ul>
                    <tfoot id='6Mqte'></tfoot>