python-3.x - 当我向 Pyjnius 提出请求时,手机没有振动
问题描述
我试图让我的手机在按下按钮时振动,但我无法让它工作。
当我这样做vibrator.vibrate(1000)
时,我的控制台中出现错误:
04-29 15:11:45.103 27494 27585 I python : Exception in thread Thread-1:
04-29 15:11:45.103 27494 27585 I python : Traceback (most recent call last):
04-29 15:11:45.104 27494 27585 I python : File "/home/alexandrumarcel/projects/kivy-apps/rextester/.buildozer/android/platform/build/build/other_builds/python3-libffi-openssl-sqlite3/armeabi-v7a__ndk_target_21/python3/Lib/threading.py", line 917, in _bootstrap_inner
04-29 15:11:45.104 27494 27585 I python : File "/home/alexandrumarcel/projects/kivy-apps/rextester/.buildozer/android/platform/build/build/python-installs/rextesterdroid/jnius/__init__.py", line 54, in jnius_thread_hook
04-29 15:11:45.104 27494 27585 I python : File "/home/alexandrumarcel/projects/kivy-apps/rextester/.buildozer/android/platform/build/build/other_builds/python3-libffi-openssl-sqlite3/armeabi-v7a__ndk_target_21/python3/Lib/threading.py", line 865, in run
04-29 15:11:45.105 27494 27585 I python : File "/home/alexandrumarcel/projects/kivy-apps/rextester/.buildozer/android/app/main.py", line 117, in execute
04-29 15:11:45.105 27494 27585 I python : File "jnius/jnius_export_class.pxi", line 742, in jnius.jnius.JavaMethod.__call__
04-29 15:11:45.105 27494 27585 I python : jnius.jnius.JavaException: Invalid call, number of argument mismatch, got 1 need 4
我尝试添加多个值(vibrator.vibrate(0, 400, 200, 300)
但它产生了另一个错误:
04-29 15:21:19.065 27996 28101 F .rextesterdroi: java_vm_ext.cc:542] native: #10 pc 00036b4f /data/data/demo.org.rextesterdroid/files/app/_python_bundle/site-packages/jnius/jnius.so (???)
04-29 15:21:19.308 27996 28101 F .rextesterdroi: runtime.cc:558] native: #15 pc 00036b4f /data/data/demo.org.rextesterdroid/files/app/_python_bundle/site-packages/jnius/jnius.so (???)
04-29 15:21:19.311 27996 28101 F .rextesterdroi: runtime.cc:558] native: #04 pc 0000eb24 /data/data/demo.org.rextesterdroid/files/app/_python_bundle/modules/_ctypes.cpython-37m.so (_ctypes_callproc+728)
04-29 15:21:19.311 27996 28101 F .rextesterdroi: runtime.cc:558] native: #13 pc 00036b4f /data/data/demo.org.rextesterdroid/files/app/_python_bundle/site-packages/jnius/jnius.so (???)
04-29 15:21:19.311 27996 28101 F .rextesterdroi: runtime.cc:566] native: #10 pc 00036b4f /data/data/demo.org.rextesterdroid/files/app/_python_bundle/site-packages/jnius/jnius.so (???)
04-29 15:21:19.595 28105 28105 I crash_dump32: type=1400 audit(0.0:355029): avc: denied { open } for path="/data/data/demo.org.rextesterdroid/files/app/_python_bundle/site-packages/jnius/jnius.so" dev="mmcblk0p60" ino=2363865 scontext=u:r:crash_dump:s0:c512,c768 tcontext=u:object_r:app_data_file:s0:c512,c768 tclass=file permissive=1
04-29 15:21:19.595 28105 28105 I crash_dump32: type=1400 audit(0.0:355030): avc: denied { getattr } for path="/data/data/demo.org.rextesterdroid/files/app/_python_bundle/site-packages/jnius/jnius.so" dev="mmcblk0p60" ino=2363865 scontext=u:r:crash_dump:s0:c512,c768 tcontext=u:object_r:app_data_file:s0:c512,c768 tclass=file permissive=1
04-29 15:21:19.603 28105 28105 F DEBUG : #10 pc 00036b4f /data/data/demo.org.rextesterdroid/files/app/_python_bundle/site-packages/jnius/jnius.so
from jnius import autoclass
PythonActivity = autoclass('org.kivy.android.PythonActivity')
Context = autoclass('android.content.Context')
activity = PythonActivity.mActivity
vibrator = activity.getSystemService(Context.VIBRATOR_SERVICE)
解决方案
从 android api 25 到 26 发生了一些变化。我遇到了同样的问题,不得不在网上搜索解决方案。与旧方法的不同之处在于,您必须获取系统服务并将其与振动器类一起投射以接收有效的振动器对象。第二个变化是 vibrate 方法的参数。现在它使用其中一种方法获取振动效果对象。CreateOneShot 只是您可以使用的一种方法。您还可以创建波形或合成(有关更多信息,请参见下面的链接)。
Vibration_Effect --> https://developer.android.com/reference/android/os/VibrationEffect
我希望,我解释它是可以理解的,它也适用于你。
例子
from jnius import autoclass, cast
PythonActivity = autoclass('org.kivy.android.PythonActivity')
activity = PythonActivity.mActivity
Context = autoclass('android.content.Context')
vibrator_service = activity.getSystemService(Context.VIBRATOR_SERVICE)
vibrator = cast("android.os.Vibrator", vibrator_service)
vibration_effect = autoclass("android.os.VibrationEffect")
# first argument of createOneShot is the time in ms
# second argument is the amplitude (range from 1 to 255), -1 is device standard amplitude
vibrator.vibrate(vibration_effect.createOneShot(2000, 150))
并且不要忘记在您的规范文件中添加振动权限!
buildozer.spec
# (list) Permissions
android.permissions = VIBRATE
推荐阅读
- python - 如何使用 pipetools 为以功能范式编写的函数编写文档字符串
- javascript - 需要获取总的 updatedPriceComplete 和 updatedPricePartial 随 $i 变化
- c++ - 带有 std::stringstream 的 C++ 模板特化
- node.js - 无法使用 node-tesseract-ocr 获取 base64image 的文本
- regex - 使用正则表达式模式的 subString 日志
- javascript - 如何将包含日期输入的表格导出到 excel 中而不使输入在之后无法使用?
- angular - 将 formGroup 发送到子组件并填充表单 Angular
- javascript - 我如何使用这个输出值作为输入
- python - 用户从python的下拉列表中选择值后如何显示过滤后的数据框和streamlit?
- recursion - 如何使用递归交换链表中的相邻节点?