Android Vibrate on touch?

Fofole picture Fofole · Jan 31, 2012 · Viewed 18.1k times · Source

I am trying to make my device vibrate when I touch an object on Screen. I am using this code:

 Vibrator v = (Vibrator) getSystemService(getApplicationContext().VIBRATOR_SERVICE); 
 v.vibrate(300);    

with the permission in the manifest file but I don't seem to get any results. Any suggestions? Also, my hardware supports vibrate.

Answer

Denilson Sá Maia picture Denilson Sá Maia · Dec 27, 2013

According to this answer, you can perform haptic feedback (vibrate) without asking for any extra permissions. Look at performHapticFeedback method.

View view = findViewById(...)
view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);

Note: I have not tested this code.