1.最简单的方式
// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 400 milliseconds
v.vibrate(400);
2.
vibrate(long[] pattern, int repeat) method:// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Start without a delay
// Vibrate for 100 milliseconds
// Sleep for 1000 milliseconds
long[] pattern = {0, 100, 1000};
// The '0' here means to repeat indefinitely
// '0' is actually the index at which the pattern keeps repeating from (the start)
// To repeat the pattern from any other point, you could increase the index, e.g. '1'
v.vibrate(pattern, 0);3.取消
v.cancel();
4.最后
<pre name="code" class="html"><uses-permission android:name="android.permission.VIBRATE"/>5.
// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Start without a delay
// Each element then alternates between vibrate, sleep, vibrate, sleep...
long[] pattern = {0, 100, 1000, 300, 200, 100, 500, 200, 100};
// The '-1' here means to vibrate once, as '-1' is out of bounds in the pattern array
v.vibrate(pattern, -1);ps:
private long pattern [] = {0,1000,1000,1000,1000,1000,1000};震动没有延时,休眠一秒再震动一秒,重复三次。
节选自:http://stackoverflow.com/questions/13950338/how-to-make-an-android-device-vibrate
本文介绍了在Android设备上实现不同震动效果的方法,包括简单的震动、带有模式的震动以及如何取消正在执行的震动。提供了详细的代码示例,展示了如何通过Vibrator API来定制震动模式。

3991

被折叠的 条评论
为什么被折叠?



