为什么我的手机会无限震动?

时间:2015-01-12 20:23:48

标签: java android

我试图在振动手机时使用振动模式。我正在使用:

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);

并且它不会停止振动。

  1. 如果我使用v.vibrate(longs, -1);,它根本不会振动。
  2. 如果我使用v.vibrate(longs, 0);,它根本不会振动。
  3. 如果我使用v.vibrate(longs, 2);或任何高于1的数字,它会无限期地振动。
  4. 如果我将长值更改为更高或更低,则没有区别。
  5. 我已阅读documentation和一些t u t o rials,我认为我没有完成这里有什么问题。为什么它不能正常振动?

    注意:我使用其他正确模式振动的应用程序,所以我知道这不是我手机的问题。

3 个答案:

答案 0 :(得分:5)

您应该阅读vibrate()的文档。

使用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振动间隔)。

答案 1 :(得分:2)

  

为什么它不能正常振动?

嗯...

首先,零毫秒是一个非常短的时间。用户不会注意到零毫秒内发生的振动,用户不会注意到零毫秒内发生的振动之间的差距。

其次,两毫秒是非常短的时间。你的整个模式每次传递需要6毫秒(重复值为0),或者第二次和后续传递需要4毫秒(重复值为1,因为它会跳过第一个元素你的模式,这是你的三个两毫​​秒值之一)。每秒有数百种这样的模式,用户无法辨别。

我建议你摆脱零,并使用合理的毫秒数。这是cybersam建议最终调用cancel()并思考你是否想在你的模式中使用偶数或奇数元素。

答案 2 :(得分:1)

来自this page的第二个答案的代码对我有用。它以图案振动,然后在图案完成后停止:

// 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);