How to make an Android device vibrate?

后端 未结 13 1726
长发绾君心
长发绾君心 2020-11-22 15:47

I wrote an Android application. Now, I want to make the device vibrate when a certain action occurs. How can I do this?

13条回答
  •  心在旅途
    2020-11-22 16:19

    Above answers are perfect. However I wanted to vibrate my app exactly twice on button click and this small information is missing here, hence posting for future readers like me. :)

    We have to follow as mentioned above and the only change will be in the vibrate pattern as below,

    long[] pattern = {0, 100, 1000, 300};
    v.vibrate(pattern, -1); //-1 is important
    

    This will exactly vibrate twice. As we already know

    1. 0 is for delay
    2. 100 says vibrate for 100ms for the first time
    3. next comes delay of 1000ms
    4. and post that vibrate again for 300ms

    One can go on and on mentioning delay and vibration alternatively (e.g. 0, 100, 1000, 300, 1000, 300 for 3 vibrations and so on..) but remember @Dave's word use it responsibly. :)

    Also note here that the repeat parameter is set to -1 which means the vibration will happen exactly as mentioned in the pattern. :)

提交回复
热议问题