How to make an Android device vibrate? with different frequency?
Try:
import android.os.Vibrator;
...
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
//deprecated in API 26
v.vibrate(500);
}
Note:
Don't forget to include permission in AndroidManifest.xml file:
<uses-permission android:name="android.permission.VIBRATE"/>
How to make an Android device vibrate when device show result on screen?
Vibrator v = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 1 seconds
v.vibrate(1000);
Note:
Don't forget to include permission in AndroidManifest.xml file:
<uses-permission android:name="android.permission.VIBRATE"/>
How to vibrate Android device on button click using vibrator effects using Kotlin?
You can create a fun
and use from it (Kotlin
):
fun Fragment.vibratePhone() {
val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
if (Build.VERSION.SDK_INT >= 26) {
vibrator.vibrate(VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE))
} else {
vibrator.vibrate(200)
}
}
And in your fragment
:
vibratePhone()
Finally in you manifest
:
<uses-permission android:name="android.permission.VIBRATE" />
How to force a vibrate on a android device with kotlin in a fragment class
private fun Fragment.vibratePhone() {
val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
if (Build.VERSION.SDK_INT >= 26) {
vibrator.vibrate(VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE))
} else {
vibrator.vibrate(200)
}
}
Related Topics
How to Allow All Network Connection Types Http and Https in Android (9) Pie
Why Doesn't "System.Out.Println" Work in Android
Populating a Listview Using an Arraylist
Limit Jvm Process Memory on Ubuntu
"Implements Runnable" VS "Extends Thread" in Java
How to Determine Whether an Array Contains a Particular Value in Java
How to Concatenate Two Arrays in Java
How to Get the Current Working Directory in Java
How to Tell Maven to Use the Latest Version of a Dependency
Why Doesn't Recyclerview Have Onitemclicklistener()
Sqlite Getting Nearest Locations (With Latitude and Longitude)
Execution Failed For Task :App:Compiledebugjavawithjavac in Android Studio
Why Maven Uses Jdk 1.6 But My Java -Version Is 1.7
Why Use Getters and Setters/Accessors
Converting Iso 8601-Compliant String to Java.Util.Date
How to Avoid Arrayindexoutofboundsexception or Indexoutofboundsexception