This is part of my code which vibrate for the random amount of time.
public boolean dispatchTouchEvent(MotionEvent ev) {
SharedPreferences appSettings = PreferenceManager.getDefaultSharedPreferences(this);
boolean doVibration = appSettings.getBoolean("vibrationCue", true);
// determine whether estimation or cue mode is active
if (!currentlyEstimating) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// determine random timespan for cue(s)
initCueLength();
if (doVibration)
{
loopnum = 0;
while(loopnum < 5) {
v.vibrate(cueLength);
loopnum ++;
}
}
}
}
}
I want the vibration to repeat for example for five time. But the while loop doesn’t work. Can you guide me that what can be a problem?
The problem is that
v.vibrateworks asynchronous, i.e. doesn’t wait for the specified time, so those 5 calls happen almost instantly and have the same effect like one call.To get the desired effect, define a vibration pattern:
http://android.konreu.com/developer-how-to/vibration-examples-for-android-phone-development/