I want to create an Android on click function that will make the phone vibrate for 5 sec.
Asked
Active
Viewed 9,770 times
3
-
@Inbar: in general, downvotes are not mean. They are a message that the voter did not think the question featured any research - see the alt-text on the down arrow. – halfer Mar 02 '16 at 12:40
1 Answers
6
Try this out:
Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(5000); // 5000 miliseconds = 5 seconds
And add the permision in AndroidManifest.xml
<uses-permission android:name="android.permission.VIBRATE"/>
Read more here please, this is the duplicate and easily to be found: How to make an Android devide vibrate

Community
- 1
- 1

Nikolas Charalambidis
- 40,893
- 16
- 117
- 183
-
As the method "v.vibrate(long)" is deprecated, here's a new way to implement for newer Api Level: val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as? Vibrator vibrator?.takeIf { it.hasVibrator() }?.let { val duration: Long = 5000 // 5 seconds if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val vibrationEffect = VibrationEffect.createOneShot(duration, VibrationEffect.DEFAULT_AMPLITUDE) vibrator.vibrate(vibrationEffect) } else { vibrator.vibrate(duration) } } – Fernando Perez Jun 30 '20 at 08:18