Question

I have passed a pattern for vibration and -1 for no repeat.But I want to pass 10 in place of -1(now it should repeat 10 times) then this patterns is not repeating 10 times.how to do this?currently I am using this code

Vibrator mVibrate = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
long pattern[]={0,800,200,1200,300,2000,400,4000};
// 2nd argument is for repetition pass -1 if you do not want to repeat the Vibrate
mVibrate.vibrate(pattern,-1);

but I want to do this mVibrate.vibrate(pattern,10);which is not working.

Was it helpful?

Solution

that works as expected, see 2nd parameter docs:

repeat the index into pattern at which to repeat, or -1 if you don't want to repeat.

and since your pattern doesn't have index 10 it's just ignored

OTHER TIPS

There is no possibility in Vibrator API to do something like this. A possible solution could be to do Your own listener and count how often it vibrates eg the pattern[] is gone through. But I have no idea how to do....maybe it is possible with a Timer that is exactly as long as Your pattern[] sum * 10

Your question is "How to Vibrate Android device for long time(e.g 2,5,10 minutes)?" Nobody has really answered your question, so I'll give it a try. I have written the following code to vibrate the phone indefinitely:

// get a handle on the device's vibrator
// should check if device has vibrator (omitted here)
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

// create our pattern for vibrations
// note that the documentation states that the pattern is as follows:
// [time_to_wait_before_start_vibrate, time_to_vibrate, time_to_wait_before_start_vibrate, time_to_vibrate, ...]

// the following pattern waits 0 seconds to start vibrating and 
// vibrates for one second
long vibratePattern[] = {0, 1000};

// the documentation states that the second parameter to the
// vibrate() method is the index of your pattern at which you
// would like to restart **AS IF IT WERE THE FIRST INDEX**

// note that these vibration patterns will index into my array,
// iterate through, and repeat until they are cancelled

// this will tell the vibrator to vibrate the device after
// waiting for vibratePattern[0] milliseconds and then
// vibrate the device for vibratePattern[1] milliseconds,
// then, since I have told the vibrate method to repeat starting
// at the 0th index, it will start over and wait vibratePattern[0] ms
// and then vibrate for vibratePattern[1] ms, and start over. It will
// continue doing this until Vibrate#cancel is called on your vibrator.
v.vibrate(pattern, 0); 

If you want to vibrate the device for 2, 5, or 10 minutes, you can use this code:

// 2 minutes
v.vibrate(2 * 60 * 1000);

// 5 minutes
v.vibrate(5 * 60 * 1000);

// 10 minutes
v.vibrate(10 * 60 * 1000);

And finally, if you want to just write one method to do this (as you should) you can write the following:

public void vibrateForNMinutes(Vibrator v, int numMinutesToVibrate) {
    // variable constants explicitly set for clarity
    // milliseconds per second
    long millisPerSecond = 1000;

    // seconds per minute
    long secondsPerMinute = 60;

    v.vibrate(numMinutesToVibrate * secondsPerMinute * millisPerSecond);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top