Android系统。如何从另一种方法中阻止方法的振动

时间:2014-07-22 19:16:49

标签: android android-vibration

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

1 个答案:

答案 0 :(得分:0)

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