0

我有一种方法可以发出振动脉冲。它使用 Thread.sleep() 在每个脉冲之间等待。我想输入一个 STOP 或 RESET 按钮来阻止蜂鸣声到达 vibratorDAYONE() 的末尾。我尝试使用 v.cancel 和 return() 但它仍然继续该方法。

 public void vibratorDAYONE()
{
    Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    if(!v.hasVibrator())
    {
        Toast.makeText(daybuzzer.this,
                "You need to have a vibrator on your phone for this app to work.", Toast.LENGTH_LONG).show();
    }
    else
        {
        Toast.makeText(daybuzzer.this, "Start Running.", Toast.LENGTH_LONG).show();
        }
    v.vibrate(200); //start first
    SystemClock.sleep(2000); //Length of the first run
    v.vibrate(200);     //start second part
    SystemClock.sleep(2000);
    v.vibrate(200);
    SystemClock.sleep(2000);
    v.vibrate(200);
    SystemClock.sleep(2000);
    v.vibrate(200);
    SystemClock.sleep(2000);
}

public void vibratorEnder()
{
    Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    v.cancel();
}
4

1 回答 1

0

vibratorDAYONE()在振动前放入一面旗帜以进行检查。将标志设置在vibratorEnder(). 如果设置了标志,则返回vibratorDAYONE()而不是继续。

于 2014-07-22T19:43:35.133 回答