我在用振动模式振动手机。我在用:
Vibrator v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
long[] longs = { 2, 0, 0, 0, 2, 0 , 0, 0, 2 };
v.vibrate(longs, 1);它就是不停地振动。
v.vibrate(longs, -1);,它根本不会振动。v.vibrate(longs, 0);,它根本不会振动。v.vibrate(longs, 2);或任何高于1的数字,它就会无限期地振动。我读过文档和一些文档,我认为我在这里没有做错什么。为什么它的振动不正常?
注:我使用的其他应用程序振动模式正确,所以我知道这不是我的手机问题。
发布于 2015-01-12 20:39:45
您应该阅读振动()的文档。
对于2, 0, 0, 0, 2, 0 , 0, 0, 2,您的意思是:“等待2 ms,振动0 ms,等待0 ms,振动0 ms,等待2 ms,振动0 ms,等待0 ms,振动0 ms,等待2 ms”。显然,这个模式永远不会震动,除非你重复这个模式(它有一个奇数的间隔)。
当您将-1以外的任何内容作为第二个参数传递时,将使用第二个参数作为索引重复模式到开始重复的模式中。由于您似乎从未调用v.cancel(),这种重复永远不会结束,从而导致无休止的振动(因为在重复的某个时刻,您将有非-0的振动间隔)。
发布于 2015-01-12 20:40:06
为什么它的振动不正常?
好吧..。
首先,零毫秒是一个非常短的时间。用户不会注意到零毫秒内发生的振动,用户也不会注意到零毫秒振动之间的差距。
第二,两毫秒是非常短的时间。整个模式每次传递需要6毫秒(重复值为0),第二次和后续传递则需要4毫秒(重复值为1,因为这将跳过模式的第一个元素,这是您的三毫秒值之一)。每秒有数以百计的这些模式,这是用户无法识别的。
我建议您摆脱零,并使用合理的毫秒数为其余。这是在赛博赛姆建议的基础上,即最终打电话给cancel(),并考虑你是否需要模式中的偶数或奇数元素。
发布于 2015-01-12 20:40:05
此页上第二个答案中的代码正在为我工作。它在一个模式中振动,然后在模式完成后停止:
// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Start without a delay
// Vibrate for 100 milliseconds
// Sleep for 1000 milliseconds
long[] pattern = {0, 100, 1000};
// The '0' here means to repeat indefinitely
// '0' is actually the index at which the pattern keeps repeating from (the start)
// To repeat the pattern from any other point, you could increase the index, e.g. '1'
v.vibrate(pattern, 0);https://stackoverflow.com/questions/27910151
复制相似问题