如何通过android编程使设备振动n次?

发布于 2024-11-04 19:31:18 字数 241 浏览 4 评论 0原文

谁能告诉我如何像这样振动相同的模式 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

临走之时 2024-11-11 19:31:19

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

long[] pattern = { 0, 100, 500, 100, 500, 100, 500, 100, 500, 100, 500};
vibrator.vibrate(pattern , -1);

I found the solution, it was very simple:

long[] pattern = { 0, 100, 500, 100, 500, 100, 500, 100, 500, 100, 500};
vibrator.vibrate(pattern , -1);
人生戏 2024-11-11 19:31:19

来自: Android Vibrator#vibrate(long[ ],整数)

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

你必须初始化索引0

long[] pattern = { 0, 100, 500, 100, 500, 100, 500, 100, 500, 100, 500};
vibrator.vibrate(pattern , 0);

From: Android Vibrator#vibrate(long[], int)

To cause the pattern to repeat, pass the index into the pattern array at which to start the repeat, or -1 to disable repeating.

You have to init index 0

long[] pattern = { 0, 100, 500, 100, 500, 100, 500, 100, 500, 100, 500};
vibrator.vibrate(pattern , 0);
逆光下的微笑 2024-11-11 19:31:19

以下对我有用:

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 方法仅启动振动,但不会等到其执行。

the following works for me:

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

The vibrate method only starts the vibration, but doesn't wait until its executed.

沫离伤花 2024-11-11 19:31:19

你的代码应该可以解决问题。只要确保你有

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.

俯瞰星空 2024-11-11 19:31:19

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

stopVibration() - 终止振动或暂停计数器,从而暂停振动模​​式。

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

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.

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();
    }
}
荒芜了季节 2024-11-11 19:31:19

在方法public void vibrate (long[] patt, int repeat)中,long[]pattern遵循以下规则:long[]pattern = {pauseTime1,vibrationTime1,pauseTime2 ,vibrationTime2,pauseTime3,vibrationTime3,...} 因此,你有奇数个值,它不会工作。您必须以vibrationTime 结束该模式。
偶数个值即可完成此工作(至少 4 个值)。

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

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

long[] pattern = {0, 500, 200, 500}
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(pattern , 5);
禾厶谷欠 2024-11-11 19:31:19

我是这样做的。我根据调用者希望它振动的次数动态生成计时数组。在循环中,从 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));
    }
}

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.

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));
    }
}
别挽留 2024-11-11 19:31:19
    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();
    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();
拿命拼未来 2024-11-11 19:31:19

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

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

OUTPUT 模式将变为
0、200、500、500、0、200、500

You can apply one trick, just build pattern dynamic based on number of repetition you want.

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

Then do the call like below

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

OUTPUT Pattern would become as
0, 200, 500, 500, 0, 200, 500

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文