Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to vibrate Android device on button click using vibrator effects using Kotlin?

How to vibrate an Android device coding with Kotlin when pressing any buttons? I have used this code below, but there aren't any effects or vibrations performed.

//click listener
    imgNextBtn.setOnClickListener {
        val vibe:Vibrator = activity?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
        vibe.vibrate(500)
        Utilities.alertDialog(this,
                activity!!,
                mContent!!
    }
}

Or

 //click listener
    imgNextBtn.setOnClickListener {
        val vibe:Vibrator = activity?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
        var effect:VibrationEffect = VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE);

    vibe.vibrate(effect)
        
        Utilities.alertDialog(this,
                activity!!,
                mContent!!
    }
}

Android Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.china.openkey">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
like image 254
Davinder Goel Avatar asked Dec 19 '17 05:12

Davinder Goel


2 Answers

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" />
like image 165
jo jo Avatar answered Sep 19 '22 01:09

jo jo


Answer given by @R2R is perfect. But vibrate() method of Vibrator class is deprecated from API Level 26. So, I am giving you the updated code:

You don't have to change everything. Just update the code for vibration in MainActivity.kt.

MainActivity.kt:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main);

        val btn_click_me = findViewById(R.id.btn_vibrate) as Button
        btn_click_me.setOnClickListener {
            val vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
            if (vibrator.hasVibrator()) { // Vibrator availability checking
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE)) // New vibrate method for API Level 26 or higher
                } else {
                    vibrator.vibrate(500) // Vibrate method for below API Level 26
                }
            }
        }
    }

}
like image 45
Avijit Karmakar Avatar answered Sep 18 '22 01:09

Avijit Karmakar