3

我使用以下代码段以特定模式振动手机,但它会抛出ArrayIndexOutOfBoundsException

vibrator.vibrate(new long[] { selectedDuration, CONSTANT_DELAY }, REPEAT); 

vibrator.vibrate(VIBRATE_DURATION);

工作正常。任何指针?

4

1 回答 1

11

文档说:

如果要重复,请将索引传递到开始重复的模式中。

意味着 REPEAT 在您的情况下只允许为 0 或 1。

这是实现:

public void vibrate(long[] pattern, int repeat)
{
    // catch this here because the server will do nothing.  pattern may
    // not be null, let that be checked, because the server will drop it
    // anyway
    if (repeat < pattern.length) {
        try {
            mService.vibratePattern(pattern, repeat, mToken);
        } catch (RemoteException e) {
        }
    } else {
        throw new ArrayIndexOutOfBoundsException();
    }
}
于 2011-02-06T19:05:38.757 回答