如何使用 Kotlin 使用振动器效果在单击按钮时振动 Android 设备?

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

如何在按下任何按钮时使用 Kotlin 振动 Android 设备编码?我在下面使用了这段代码,但没有执行任何效果或振动。

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

或者

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

使用 kotlin 工作示例的振动试试这个

Manifest.xml

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

在科特林中:

 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)

        }
    }
}

@R2R 的回答很完美。但是 Vibrator class 的 vibrate() 方法已从 API 级别 26 弃用 。所以,我给你更新的代码:

您不必更改所有内容。只需在 MainActivity.kt.

中更新 vibration 的代码

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

}

您可以创建一个 fun 并使用它 (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)
    }
}

在你的 fragment 中:

vibratePhone()

终于在你身边manifest:

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

简化它可能像

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)
            }
        }

什么时候模式可以像

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

嗡嗡声和非嗡嗡声的元素 -> 等待 0 毫秒,嗡嗡声 200 毫秒,等待 100 毫秒,嗡嗡声 300 毫秒

当然要在 Manifest 中获得许可

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

希望对以后的人有所帮助