人造人。如何阻止另一种方法对方法的振动



我有一个方法可以发出振动脉冲。它使用Thread.sleep()在每个脉冲之间等待。我想输入一个停止或重置按钮,以阻止蜂鸣声到达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();
}

vibratorDAYONE()中放置一个标志,以便在振动之前进行检查。在vibratorEnder()内设置标志。如果设置了标志,则从vibratorDAYONE()返回,而不是继续。

最新更新