Tom Tom - 1 year ago 47
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);

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 Source

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() {
   }, 0, 1000);