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

时间:2017-12-19 05:22:29

标签: android kotlin

按任意按钮时如何振动设备。 我使用过此代码,但没有任何影响或振动

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

menifest:`

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

4 个答案:

答案 0 :(得分:6)

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

您无需更改所有内容。只需在 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
                }
            }
        }
    }

}

答案 1 :(得分:2)

使用kotlin工作样品进行振动试试这个

Manifest.xml

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

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

        }
    }
}

答案 2 :(得分:2)

您可以创建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" />

答案 3 :(得分:2)

简化它就像

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毫秒

当然可以在清单中获得许可

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

希望,将来会对别人有帮助