Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 249 Vote(s) - 3.61 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to vibrate Android device on button click using vibrator effects using Kotlin?

#1
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" />
Reply

#2
Vibration using kotlin working sample try this

Manifest.xml


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

In kotlin:

val vibratorService = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vibratorService.vibrate(500)

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<Button
android:id="@+id/btn_vibrate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vibrate"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>


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 vibratorService = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vibratorService.vibrate(500)

}
}
}
Reply

#3
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" />

Reply

#4
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
}
}
}
}

}
Reply

#5
Simplify it could be like

val buzzer = activity?.getSystemService<Vibrator>()

buzzer?.let {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
buzzer.vibrate(VibrationEffect.createWaveform(pattern, -1))
} else {
//deprecated in API 26
buzzer.vibrate(pattern, -1)
}
}

When pattern could be like

val pattern = longArrayOf(0, 200, 100, 300)

Elements of buzzing and non-buzzing takes -> wait 0 ms, buzz 200ms, wait 100ms, buzz 300ms

Of course with permission in Manifest

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

Hope, it will help somebody in future

Reply

#6
Vibrator Service is deprecated for API 31 and above.
To do this the right way, do the following instead as addressed in [this question][1]:

fun vibrate(){
val vib = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val vibratorManager =
getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
vibratorManager.defaultVibrator
} else {
@Suppress("DEPRECATION")
getSystemService(VIBRATOR_SERVICE) as Vibrator
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vib.vibrate(VibrationEffect.createOneShot(300, VibrationEffect.DEFAULT_AMPLITUDE) )
}else{
@Suppress("DEPRECATION")
vib.vibrate(300)
}
}


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through