1

I want the phone to vibrate when I click the button

I want to do this with Kotlin in Android Studio

The following code did not work

val vib = getSystemService(MainActivity.VIBRATOR_SERVICE) as Vibrator
val milliseconds:Long = 2000
vib.vibrate(milliseconds)
Philippe
  • 1,715
  • 4
  • 25
  • 49
Pedram 7t
  • 11
  • 1

1 Answers1

0

Add vibrator permission to AndroidManifest.xml:

<uses-permission android:name="android.permission.VIBRATE"/>

then..

val vibrator:Vibrator
vibrator = getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vibrator.vibrate(100)
Pedram 7t
  • 11
  • 1