11

谁能告诉我如何像这样振动相同的模式 5 次我的模式

long[] pattern = { 0, 200, 500 };

我希望这种模式重复 5 次

Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(pattern , 5);
4

9 回答 9

22

我找到了解决方案,非常简单:

long[] pattern = { 0, 100, 500, 100, 500, 100, 500, 100, 500, 100, 500};
vibrator.vibrate(pattern , -1);
于 2011-05-12T10:39:30.700 回答
5

来自:Android 振动器#vibrate(long[], int)

要使模式重复,请将索引传递到开始重复的模式数组中,或 -1 以禁用重复。

你必须初始化索引 0

long[] pattern = { 0, 100, 500, 100, 500, 100, 500, 100, 500, 100, 500};
vibrator.vibrate(pattern , 0);
于 2017-09-07T21:06:32.630 回答
4

以下对我有用:

if(vibration_enabled) {
    final Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    if(v.hasVibrator()) {
        final long[] pattern = {0, 1000, 1000, 1000, 1000};
        new Thread(){
            @Override
            public void run() {
                for(int i = 0; i < 5; i++){ //repeat the pattern 5 times
                    v.vibrate(pattern, -1);
                    try {
                       Thread.sleep(4000); //the time, the complete pattern needs
                    } catch (InterruptedException e) {
                        e.printStackTrace();  
                    }
                }
            }
        }.start();
    }
}

vibrate 方法只开始振动,但不等到它执行。

于 2013-09-09T22:36:17.423 回答
3

您的代码应该可以解决问题。只要确保你 <uses-permission android:name="android.permission.VIBRATE"/>AndroidManifest.xml文件中。

于 2011-05-04T07:57:55.803 回答
2

除了上面给出的解决方案之外,我还创建了自己的振动模式,我可以在其中控制振动之间的持续时间大小。startVibration() 创建一分钟的连续规则振动模式。

stopVibration() - 终止振动或暂停 counterTimer 从而暂停振动模​​式。

private time = 0;
private countDownTimer;

private void startVibration() {
    time = (int) System.currentTimeMillis();

    countDownTimer = new CountDownTimer(60000, 1000) {

        public void onTick(long millisUntilFinished) {

            time = (int) (millisUntilFinished / 1000);
            int[] timeLapse = {58, 55, 52, 49, 46, 43, 40, 37, 34, 31, 28, 25, 22, 19, 16, 13, 10, 7, 4, 1};
            for (int k = 0; k < timeLapse.length; k++) {
                if (time == timeLapse[k]) {
                    ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(1000);
                }
            }
        }

        public void onFinish() {
        }
    }.start();
}

private void stopVibration() {
    if (countDownTimer != null) {
        countDownTimer.cancel();
    }
}
于 2017-07-03T08:28:04.160 回答
0

这就是我的做法。我根据调用者希望它振动的次数动态生成我的计时数组。在循环中,从 1 开始,以避免 0 % 2 在结束时造成额外的无用延迟。

private void vibrate(int times)
{
    Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
    {
        ArrayList<Long> timings = new ArrayList();

        timings.add(0L);

        for(int i = 1; i <= times; i ++)
        {

            if(i%2==0)
                timings.add(0L);

            timings.add(250L);


        }

        long[] arrTimings = new long[timings.size()];

        for(int j = 0; j < timings.size(); j++)
        {
            arrTimings[j] = timings.get(j);
        }

        vibrator.vibrate(VibrationEffect.createWaveform(arrTimings, -1));
    }
}
于 2019-02-13T21:07:14.717 回答
0
    long vibrationDuration = Arrays.stream(pattern).sum();
    new CountDownTimer(vibrationDuration*(repeat+1), vibrationDuration) {

        @Override
        public void onTick(long millisUntilFinished) {
            v.cancel();
            if (Build.VERSION.SDK_INT >= 26) {
                VibrationEffect vibrationEffect = VibrationEffect.createWaveform(pattern, -1);
                v.vibrate(vibrationEffect);
            } else {
                v.vibrate(pattern, -1);
            }
        }

        @Override
        public void onFinish() {
            v.cancel();
        }
    }.start();
于 2019-08-23T10:19:24.703 回答
0

public void vibrate (long[] pattern, int repeat)方法中,long[] 模式遵循以下规则: long[] pattern = {pauseTime1,vibrationTime1, pauseTime2,vibrationTime2, pauseTime3,vibrationTime3, ...}结果你有奇数个值,它不起作用。您必须以振动时间结束模式。偶数个值完成这项工作(至少 4 个值)。

long[] pattern = {0, 500, 200, 500}
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(pattern , 5);
于 2018-12-07T09:22:05.153 回答
0

您可以应用一种技巧,只需根据您想要的重复次数构建动态模式。

private long[] createVibrationPattern(long[] oneShotPattern, int repeat) {
    long[] repeatPattern = new long[oneShotPattern.length * repeat];
    System.arraycopy(oneShotPattern, 0, repeatPattern, 0, oneShotPattern.length);
    for (int count = 1; count < repeat; count++) {
        repeatPattern[oneShotPattern.length * count] = 500; // Delay in ms, change whatever you want for each repition
        System.arraycopy(oneShotPattern, 1, repeatPattern, oneShotPattern.length * count + 1, oneShotPattern.length - 1);
    }
    return repeatPattern;
}

然后像下面一样拨打电话

long[] pattern = { 0, 200, 500 };
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(createVibrationPattern(pattern , 2);

输出模式将变为 0, 200, 500, 500 , 0, 200, 500

于 2020-07-17T13:06:35.920 回答