Tom Tom - 1 month ago 11
Android Question

Android - How do i make a small pause between two vibrations

i have created an application and whenever the user receives new notification i would like to vibrate his phone.

I have found solution here:

Vibrator v = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);


But i would like to make a vibration for example 500ms, then wait 500ms and then make another 500ms vibration. Unfortunately i dont want to use Sleep, and "Freeze" my entire program with it for half a second.

what would be the best approach/solution for this ? is it okay to just sleep the device for hundreds of miliseconds ?


Answer

It is never acceptable to sleep / hang or even delay your UI (User Interface) Thread. You can use a Timer for your task:

Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
   public void run() {
      v.vibrate(500);
      }
   }, 0, 1000);
}
Comments