Continuous Vibration on Touch in android

浪子不回头ぞ 提交于 2019-12-06 16:46:29

First of all, you always have to specify the vibration time in millis. So you can set a long time for the vibration and stop the vibration in the ACTION_UP event. For example:

public boolean onTouch(View v, MotionEvent event) {
    Vibrator vb = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        vb.vibrate(10000); // 10 seconds
        System.out.println("ACTION_DOWN");

        return true;
    case MotionEvent.ACTION_MOVE:
        System.out.println("ACTION_MOVE");

        break;
    case MotionEvent.ACTION_UP:
        System.out.println("ACTION_UP");
        vb.cancel(); // Stop the vibration
        break;
    default:
        return false;
    }

    return true;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!