How to vibrate device n number of times through programming in android?

14,848

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

Solution 2

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

Solution 3

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.

Solution 4

Your code should do the trick. Just make sure you have <uses-permission android:name="android.permission.VIBRATE"/> in the AndroidManifest.xml file.

Solution 5

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();
    }
}
Share:
14,848

Related videos on Youtube

Gkapoor
Author by

Gkapoor

Updated on June 04, 2022

Comments

  • Gkapoor
    Gkapoor almost 2 years

    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);
    
  • Gkapoor
    Gkapoor almost 13 years
    its already working but i dont know how to vibrate it n number of times like if select 5 on runtime then it should vibrate 5 times...
  • Gabriel Negut
    Gabriel Negut almost 13 years
    for(int i = 0; i < n; i++) vibrator.vibrate(pattern, -1);
  • Anis LOUNIS aka AnixPasBesoin
    Anis LOUNIS aka AnixPasBesoin over 7 years
    Looks like a hack ^^
  • Anis LOUNIS aka AnixPasBesoin
    Anis LOUNIS aka AnixPasBesoin over 7 years
    Looks like another hack. What is the second argument of the vibrate() method for?
  • rbs
    rbs over 7 years
    Documentation for the vibrate-method , second arg: repeat count, -1 means dont repeat
  • Ramkesh Yadav
    Ramkesh Yadav over 6 years
    great hack nice :)
  • Johnny Five
    Johnny Five almost 5 years
    Why is it a hack? There is a pattern parameter and there is a number of repeating parameter. vibrate(...) method documentation says To cause the pattern to repeat, pass the index into the pattern array at which to start the repeat, or -1 to disable repeating. Everything done according to documentation.
  • 1737973
    1737973 over 4 years
    Brackets and parens aren't balanced here, are them?
  • Mayank Sharma
    Mayank Sharma over 4 years
    Bad practice! No need to run loops to achieve repetition of vibrator