問題描述
我發現 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模板網!