0

我正在开发一个 android 游戏,我在其中使用此代码进行振动

public void gameover() {
    prefs = activity.getSharedPreferences("AUTHENTICATION_FILE_NAME", Context.MODE_WORLD_WRITEABLE);
    String vibration = prefs.getString("vibration", null);
    if(vibration != null) {
        if(vibration.equals("on")) {
            Vibrator v = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE);
            v.vibrate(100);
        }
    }
}

但它在不断地振动。我想在 100 毫秒后停止这种振动,但它并没有停止。我应该怎么办?

4

2 回答 2

0

试试这个..它会帮助你..

Vibrator v = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE);
v.cancel();
于 2014-04-26T04:57:53.353 回答
0

首先做振动器的对象

Vibrator vb = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

然后在按钮的 onclick 事件上声明此代码。

long[] pattern = {0, 1000, 0};
                vb.vibrate(pattern, 0);

取消振动然后使用

vb.cancel();
于 2014-04-26T05:00:53.150 回答