我有一种方法可以发出振动脉冲。它使用 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();
}