Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my phone vibrate indefinitely?

Tags:

java

android

I'm trying to use a vibration pattern when vibrating the phone. I'm using:

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.

  1. If I use v.vibrate(longs, -1); it doesn't vibrate at all.
  2. If I use v.vibrate(longs, 0); it doesn't vibrate at all.
  3. If I use v.vibrate(longs, 2); or any number higher than 1, it vibrates indefinitely.
  4. If I change the long values to so they're higher or lower it makes no difference.

I've read the documentation and some tutorials and I don't think I've done anything wrong here. Why isn't it vibrating properly?

Note: I use other apps that vibrate in patterns properly so I know it's not an issue with my phone.

like image 927
RǢF Avatar asked Dec 02 '25 03:12

RǢF


1 Answers

You should read the documentation for vibrate().

With 2, 0, 0, 0, 2, 0 , 0, 0, 2, you are saying: "wait for 2 ms, vibrate for 0 ms, wait for 0 ms, vibrate for 0 ms, wait for 2 ms, vibrate for 0 ms, wait for 0 ms, vibrate for 0 ms, wait for 2 ms". 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 at which to start repeating. Since you never seem to call v.cancel(), this repetition never ends, causing endless vibration (because at some point in the repetition, you will have non-0 vibration interval).

like image 148
cybersam Avatar answered Dec 03 '25 18:12

cybersam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!