如何通过android编程使设备振动n次?
谁能告诉我如何像这样振动相同的模式 5 次我的模式
long[] pattern = { 0, 200, 500 };
我希望这个模式重复 5 次
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(pattern , 5);
can anyone tell me how to vibrate same patter 5 times like this my pattern
long[] pattern = { 0, 200, 500 };
i want this pattern to repeat 5 times
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(pattern , 5);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我找到了解决方案,非常简单:
I found the solution, it was very simple:
来自: Android Vibrator#vibrate(long[ ],整数)
你必须初始化索引0
From: Android Vibrator#vibrate(long[], int)
You have to init index 0
以下对我有用:
vibrate 方法仅启动振动,但不会等到其执行。
the following works for me:
The vibrate method only starts the vibration, but doesn't wait until its executed.
你的代码应该可以解决问题。只要确保你有
在
AndroidManifest.xml
文件中。Your code should do the trick. Just make sure you have
<uses-permission android:name="android.permission.VIBRATE"/>
in the
AndroidManifest.xml
file.除了上面给出的解决方案之外,我还创建了自己的振动模式,可以控制振动之间的持续时间大小。 startVibration() 创建一分钟的连续规则振动模式。
stopVibration() - 终止振动或暂停计数器,从而暂停振动模式。
Besides the above given solutions, i have created my own vibration pattern where i can control the duration size between vibrations. startVibration() creates a continous regular vibration pattern for one minute.
stopVibration() - Terminates the vibration or pauses the counterTimer thus pausing the vibration pattern.
在方法public void vibrate (long[] patt, int repeat)中,long[]pattern遵循以下规则:long[]pattern = {pauseTime1,vibrationTime1,pauseTime2 ,vibrationTime2,pauseTime3,vibrationTime3,...} 因此,你有奇数个值,它不会工作。您必须以vibrationTime 结束该模式。
偶数个值即可完成此工作(至少 4 个值)。
In the method public void vibrate (long[] pattern, int repeat), the long[] pattern follows the rule : long[] pattern = {pauseTime1, vibrationTime1, pauseTime2, vibrationTime2, pauseTime3, vibrationTime3, ...} so as a result you have odd number of values, it won't works. You must have to end the pattern with a vibrationTime.
An even number of value do the job (at least 4 values).
我是这样做的。我根据调用者希望它振动的次数动态生成计时数组。在循环中,从 1 开始,以避免 0 % 2 在最后造成额外的无用延迟。
Here's how I did it. I dynamically generate my timing array based on the number of times the caller wants it to vibrate. In the loop, start at 1 so to avoid 0 % 2 causing an extra useless delay at the end.
您可以应用一种技巧,只需根据您想要的重复次数构建动态模式即可。
然后执行如下调用
OUTPUT 模式将变为
0、200、500、500、0、200、500
You can apply one trick, just build pattern dynamic based on number of repetition you want.
Then do the call like below
OUTPUT Pattern would become as
0, 200, 500, 500, 0, 200, 500