【问题标题】:How to Vibrate Android device for long time(e.g 2,5,10 minutes)如何长时间振动 Android 设备(例如 2、5、10 分钟)
【发布时间】:2013-06-20 07:26:17
【问题描述】:

我传递了一个振动模式,-1 表示不重复。但我想传递 10 代替 -1(现在它应该重复 10 次)然后这个模式不重复 10 次。怎么做?目前我正在使用此代码

Vibrator mVibrate = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
long pattern[]={0,800,200,1200,300,2000,400,4000};
// 2nd argument is for repetition pass -1 if you do not want to repeat the Vibrate
mVibrate.vibrate(pattern,-1);

但我想这样做mVibrate.vibrate(pattern,10);这是行不通的。

【问题讨论】:

    标签: android android-vibration


    【解决方案1】:

    按预期工作,请参阅第二个参数文档:

    重复 要重复的模式的索引,如果不想重复,则为 -1。

    并且由于您的模式没有索引 10,因此它被忽略了

    【讨论】:

    • 我们说的是索引,所以 int[4] arr 有索引:0.1 2 3
    • 现在我将索引传递为 7 并振动一次(20 到 25 秒)直到模式运行,但我的问题是..如何振动 5 或 10 分钟?
    • Vibraror 类中没有这样的 api 调用,所以让它永远振动并在延迟后取消()它
    • 但它不会一直振动。它会在一段时间后停止振动。告诉我如何永远振动它,然后我会根据我指定的时间取消它。
    • 说实话,非常感谢 androud 开发人员,如果我的手机振动 20 分钟,我会疯掉的
    【解决方案2】:

    在 Vibrator API 中不可能做这样的事情。一个可能的解决方案可能是做你自己的监听器并计算它的振动频率,例如 pattern[] 是通过的。但我不知道该怎么做....也许有一个与您的模式[] sum * 10 一样长的 Timer

    【讨论】:

      【解决方案3】:

      您的问题是“如何长时间(例如 2、5、10 分钟)振动 Android 设备?”没有人真正回答你的问题,所以我会试一试。我已经编写了以下代码来无限期地振动手机:

      // get a handle on the device's vibrator
      // should check if device has vibrator (omitted here)
      Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
      
      // create our pattern for vibrations
      // note that the documentation states that the pattern is as follows:
      // [time_to_wait_before_start_vibrate, time_to_vibrate, time_to_wait_before_start_vibrate, time_to_vibrate, ...]
      
      // the following pattern waits 0 seconds to start vibrating and 
      // vibrates for one second
      long vibratePattern[] = {0, 1000};
      
      // the documentation states that the second parameter to the
      // vibrate() method is the index of your pattern at which you
      // would like to restart **AS IF IT WERE THE FIRST INDEX**
      
      // note that these vibration patterns will index into my array,
      // iterate through, and repeat until they are cancelled
      
      // this will tell the vibrator to vibrate the device after
      // waiting for vibratePattern[0] milliseconds and then
      // vibrate the device for vibratePattern[1] milliseconds,
      // then, since I have told the vibrate method to repeat starting
      // at the 0th index, it will start over and wait vibratePattern[0] ms
      // and then vibrate for vibratePattern[1] ms, and start over. It will
      // continue doing this until Vibrate#cancel is called on your vibrator.
      v.vibrate(pattern, 0); 
      

      如果您想振动设备 2、5 或 10 分钟,您可以使用以下代码:

      // 2 minutes
      v.vibrate(2 * 60 * 1000);
      
      // 5 minutes
      v.vibrate(5 * 60 * 1000);
      
      // 10 minutes
      v.vibrate(10 * 60 * 1000);
      

      最后,如果您只想编写一种方法来执行此操作(您应该这样做),您可以编写以下代码:

      public void vibrateForNMinutes(Vibrator v, int numMinutesToVibrate) {
          // variable constants explicitly set for clarity
          // milliseconds per second
          long millisPerSecond = 1000;
      
          // seconds per minute
          long secondsPerMinute = 60;
      
          v.vibrate(numMinutesToVibrate * secondsPerMinute * millisPerSecond);
      }
      

      【讨论】:

        猜你喜欢
        • 2020-07-30
        • 1970-01-01
        • 2014-02-03
        • 2018-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-27
        • 1970-01-01
        相关资源
        最近更新 更多