【问题标题】:How to handle android runtime permission with kivy如何用kivy处理android运行时权限
【发布时间】:2018-05-10 15:19:21
【问题描述】:

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

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

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

【问题讨论】:

    标签: android python kivy android-permissions pyjnius


    【解决方案1】:

    pyjnius 是要走的路。您必须使用 pyjnius 移植 these instructions。这涉及以下步骤:

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

      android.add_aars = support-v4-26.0.0-alpha1.aar  
      
    • 确保 jinius 符合 buildozer.spec 中的要求

    • 使用下面的代码sn-p

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

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

    这里是获取权限的函数:

    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 来做到这一点,但现在这个是为我做的。

    【讨论】:

    • 嘿,我正在尝试使用 Buildozer 从 Kivy Python Android 中的存储中运行一个简单的视频,而当相机工作时,视频仅加载黑色。你知道这是为什么吗?我遇到了您的答案,因为我认为这是一个许可问题。这是我的问题stackoverflow.com/questions/65042155/…。但由于相机加载可能是别的东西。谢谢!
    【解决方案2】:

    python-for-android 没有任何处理运行时权限的代码。我希望尽快看到它,但它没有预计到达时间。

    如果您有兴趣并知道如何操作,您可以自己添加代码。如果您想尝试一下,非常欢迎这样的贡献。

    【讨论】:

    • 任何提供一点指导的东西都会非常有帮助,因为你知道我是这个 Kivy android 或 python-for-android 的新手。
    【解决方案3】:

    我知道这个答案有点晚了,但要获得权限,您必须在构建之前指定它们。例如 buildozer 使用 buildozer.spec。在这个文件中你可以指定你需要的权限。

    【讨论】:

      【解决方案4】:

      您好,这个问题很老,但您可以使用

      request_permissions([Permission.WRITE_EXTERNAL_STORAGE])
      #For requesting permission you can pass a list with all the permissions you need
      
      check_permission('android.permission.WRITE_EXTERNAL_STORAGE')
      #returns True if you have the permission 
      

      您可以查看:python-for-android example

      您可以检查代码和您可以使用此方法的权限列表: python-for-android code

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-11
        • 2017-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-23
        • 2020-05-29
        相关资源
        最近更新 更多