当我向 Pyjnius 提出请求时,手机没有振动

Posted

技术标签:

【中文标题】当我向 Pyjnius 提出请求时,手机没有振动【英文标题】:Phone doesn't vibrate when I request it with Pyjnius 【发布时间】:2019-09-18 01:50:27 【问题描述】:

我试图让我的手机在按下按钮时振动,但我无法让它工作。

当我执行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)

【问题讨论】:

【参考方案1】:
from jnius import cast
from jnius import autoclass
PythonActivity = autoclass('org.renpy.android.PythonActivity')
activity = PythonActivity.mActivity

Context = autoclass('android.content.Context')
vibrator = activity.getSystemService(Context.VIBRATOR_SERVICE)

vibrator.vibrate(10000) 

您错过了“vibrator.vibrate(10000)”行!

【讨论】:

【参考方案2】:

从 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

【讨论】:

以上是关于当我向 Pyjnius 提出请求时,手机没有振动的主要内容,如果未能解决你的问题,请参考以下文章

当应用程序在后台并且手机处于振动状态时,一些闹钟应用程序(例如 Alarmy)如何能够在 iPhone 上播放声音

iphone锁定时带有声音和振动的通知

长时间振动?

oppoa 55返回键去掉振动还有显示箭头怎么弄

如何为特定来电提供自定义振动

405使用mod-rewrite对PHP脚本的PUT请求出错