有没有办法在它们之间进行300ms的说明?

发布于 2025-01-27 18:59:30 字数 741 浏览 3 评论 0原文

我有一个问题:我应该以500ms *,300ms的暂停,500ms *的振动,300ms的暂停和最后500毫秒 *的振动来振动手机。我尝试使用处理程序,但不幸的是,就像他们在一个等待时间内加起来一样。是否有一种特定的方法可以顺序进行所有这些操作,并通过它们之间的延迟进行延迟?一千个感谢。

时间因许多因素而异

    val vibration = requireActivity().getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
    Handler(Looper.getMainLooper()).postDelayed({
        vibration.vibrate(code.code.duration1.toLong())
    }, 600)
    Handler(Looper.getMainLooper()).postDelayed({
        vibration.vibrate(code.code.duration2.toLong())
    }, 2000)
    Handler(Looper.getMainLooper()).postDelayed({
        vibration.vibrate(code.code.duration3.toLong())
        id.setImageDrawable(AppCompatResources.getDrawable(requireActivity(), R.drawable.ic_play))
    }, 3600)

I have this problem: I should vibrate the phone for 500ms *, 300ms of pause, 500ms * of vibration, 300ms of pause and finally 500ms * of vibration. I tried using Handler but unfortunately it's like they add up in one wait time. Is there a specific way to do all these operations sequentially and by putting a delay between them? A thousand thanks.

Time varies depending on many factors

    val vibration = requireActivity().getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
    Handler(Looper.getMainLooper()).postDelayed({
        vibration.vibrate(code.code.duration1.toLong())
    }, 600)
    Handler(Looper.getMainLooper()).postDelayed({
        vibration.vibrate(code.code.duration2.toLong())
    }, 2000)
    Handler(Looper.getMainLooper()).postDelayed({
        vibration.vibrate(code.code.duration3.toLong())
        id.setImageDrawable(AppCompatResources.getDrawable(requireActivity(), R.drawable.ic_play))
    }, 3600)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落在眉间の轻吻 2025-02-03 18:59:30

尝试以下代码,这对我来说非常有用。

fun Fragment.vibratePhone() {
    val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            vibrator.vibrate(
                VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK),
                AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setHapticChannelsMuted(false)
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .build()
            )
        } else {
            vibrator.vibrate(
                VibrationEffect.createOneShot(VIBRATION_TIME, VIBRATION_AMPLITUDE),
                AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .build()
            )
        }
    } else {
        vibrator.vibrate(longArrayOf(0, 100, 50, 100, 50, 100), -1)
    }
}

在上面的代码中,您会看到,

  • 0表示延迟
  • 100表示​​100ms的振动
  • 50表示延迟
  • 100表示​​100ms等振动等。
  • 重复:最后-1表示振动将以您定义的模式发生,并且不会重复其他定义几次要重复它。

Try the below code, which is working for me perfectly.

fun Fragment.vibratePhone() {
    val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            vibrator.vibrate(
                VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK),
                AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setHapticChannelsMuted(false)
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .build()
            )
        } else {
            vibrator.vibrate(
                VibrationEffect.createOneShot(VIBRATION_TIME, VIBRATION_AMPLITUDE),
                AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .build()
            )
        }
    } else {
        vibrator.vibrate(longArrayOf(0, 100, 50, 100, 50, 100), -1)
    }
}

In the above code, you will see that,

  • 0 means delay
  • 100 means vibrate for 100ms
  • 50 means delay
  • 100 means vibrate for 100ms and so on.
  • Repetition: at last -1 means vibration will happen in the pattern you have defined and won't repeat else define several times you want to repeat it.
十年九夏 2025-02-03 18:59:30

您可以使用更可读的Coroutine,而不是使用处理程序或线程。

myScope.launch(Dispatchers.Main) {
    vibration.vibrate(500)
    delay(500 + 300)
    vibration.vibrate(500)
    delay(500 + 300)
    vibration.vibrate(500)
}

Instead of using Handlers or Threads, you can use a coroutine which would be more readable.

myScope.launch(Dispatchers.Main) {
    vibration.vibrate(500)
    delay(500 + 300)
    vibration.vibrate(500)
    delay(500 + 300)
    vibration.vibrate(500)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文