-1

I am just curious if my app would work out this way to make it vibrate when the phone is shook:

if(event.values[0] > 1.0) {
    vibrator.vibrate(500);
}
else {
    if(event.values[1] > 1.0)
    vibrator.vibrate(500);
}

What I am doing is SensorEventListener and I and combining 2 listeners.

Will this work out doing it this way? I am unable to test it as I don't own an Android phone, so I am asking (programming-wise) if this is possible.

admdrew
  • 3,790
  • 4
  • 27
  • 39
Rido
  • 27
  • 6

1 Answers1

1

Yes, it will work that way. Just make sure to stop handling the sensor events for the 500 milliseconds the phone is vibrating.

boolean isVibrating = false;
Handler handler = new Handler(Looper.getMainLooper());

if(!isVibrating){
    if(event.values[0] > 1.0) {
        vibrate(500);
    } else if(event.values[1] > 1.0){
        vibrate(500);
    }
}

void vibrate(long time){
    isVibrating = true;
    vibrator.vibrate(time);
    handler.postDelayed(new Runnable(){
            public void run(){
                isVibrating = false;
            }
        },
        time)
}
Corneliu Dascălu
  • 3,900
  • 1
  • 28
  • 38