Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
like image 834
Gkapoor Avatar asked May 04 '11 07:05

Gkapoor


3 Answers

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);
like image 59
Gkapoor Avatar answered Nov 13 '22 16:11

Gkapoor


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);
like image 35
Daniel Velarde Avatar answered Nov 13 '22 14:11

Daniel Velarde


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.

like image 4
rbs Avatar answered Nov 13 '22 16:11

rbs