• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Vibrator Lag

Hi everyone.

I've just implemented vibrations in my game. The problem I'm having is that almost every time it vibrates; the game lags and everything jumps over about half a second.

Here's the relevant code:

//member variable
Vibrator mVibrator;

//class constructor
mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);

//when the player gets hit
//damage is passed into the function as the amount of damage they took.
mVibrator.vibrate((long)(damage * 1000));

The only reason I could think of that would cause this is if the phone has to wait for the vibrator to finish ... which would be odd but not impossible. Does anyone know why it does this or a way around it? I would assume that putting the vibrator in its own thread would work but I'd rather not make a separate thread for it if i don't have to.
 
To keep it clean why not put the vibrator into its own class and have its thread itself? As for the vibrator causing delays, I have never noticed it however the only times I have used it outside of notifications is on menu presses and to notify the user that a screen shake worked and there was no lag that I was aware of. That said it could be a phone specific issue where it just take an abnormally long time for the vibrator service to respond. In which case you most certainly want to try threading to see if its a solution otherwise you are going to have angry users.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones