如何使用 kivy 处理 android 运行时权限 [英] How to handle android runtime permission with kivy

查看:57
本文介绍了如何使用 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 运行时权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆