Why does my phone vibrate endlessly?

I am trying to use vibration when vibrating my phone. I use:

Vibrator v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
                // Vibrate for 500 milliseconds
                long[] longs = { 2, 0, 0, 0, 2, 0 , 0, 0, 2 };
                v.vibrate(longs, 1);

      

and it just doesn't stop vibrating.

  • If I use it v.vibrate(longs, -1);

    , it doesn't vibrate at all.
  • If I use it v.vibrate(longs, 0);

    , it doesn't vibrate at all.
  • If I use v.vibrate(longs, 2);

    or any number above 1, it vibrates endlessly.
  • If I change the long values ​​to be higher or lower, it doesn't matter.

I read the documentation and t u t o rials and I don't know I think I did something wrong. Why isn't it vibrating properly?

Note. I use other apps that vibrate in patterns, so I know this is not a problem with my phone.

+3


source to share


3 answers


You should read the vibro () documentation .

With 2, 0, 0, 0, 2, 0 , 0, 0, 2

you say, "Wait 2ms, vibrate for 0ms, wait 0ms, vibrate for 0ms, wait 2ms, vibrate for 0ms, wait 0ms, vibrate 0ms, wait 2ms." Obviously, this pattern never vibrates unless you repeat the pattern (which has an odd number of intervals).



When you pass anything other than -1 as the second argument, the pattern is repeated using the second argument as the index into the pattern to start repeating at. Since you never name v.cancel()

, this repetition never ends, causing endless vibration (because at some point in repetition you will have a non-0 vibrational interval).

+5


source


Why doesn't it vibrate correctly?

Well...

First, zero milliseconds is a very short time. Users will not notice vibration that occurs in zero milliseconds, and users will not notice the gap between vibrations that occur in zero millisecond.



Secondly, two milliseconds is a very short time. Your entire pattern takes six milliseconds for each pass (with a repeating value 0

) or four milliseconds for the second and subsequent passes (with a repeated value 1

, since this skips the first element of your pattern, which is one of three three millisecond values). There are hundreds of these patterns per second that will not be visible to the user.

I suggest you get rid of the zeros and use a reasonable amount of milliseconds for the rest. This sits at the top of cybersam's ultimately defiant cancel()

suggestions and thinking about whether you want an even or odd number of elements in your template.

+2


source


The code from the second answer on this page works for me. It vibrates in the pattern and then stops after the pattern is complete:

// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

// Start without a delay
// Vibrate for 100 milliseconds
// Sleep for 1000 milliseconds
long[] pattern = {0, 100, 1000};

// The '0' here means to repeat indefinitely
// '0' is actually the index at which the pattern keeps repeating from (the start)
// To repeat the pattern from any other point, you could increase the index, e.g. '1'
v.vibrate(pattern, 0);

      

+1


source







All Articles