- 让手机震动,需要创建Vibrator对象,通过调用vibrate方法来达到震动的目的。
↳android.os.Vibrator
void | cancel() Turn the vibrator off. |
boolean | Check whether the hardware has a vibrator. |
void | vibrate(long[] pattern, int repeat) Vibrate with a given pattern. |
void | vibrate(long milliseconds) Turn the vibrator on. |
- 在Vibrate的参数中:
pattern: an array of longs of times for which to turn the vibrator on or off.
repeat: the index into pattern at which to repeat, or -1 if you don't want to repeat.
-
特别需要注意的是,在new long[]{num0,num1,num2,...}中第0、2、4...个数代表不震动的时间(ms),而奇数标号上的数代表震动的时间(ms),这样便组成了一组震动方式(pattern)
-
且,当repeat 为 0时, 持续震动。-1时,只震动一轮。
1: mVibrator01 = ( Vibrator )getApplication().getSystemService( Service .VIBRATOR_SERVICE );
2: /*设定震动的周期*/
3: mVibrator01.vibrate( new long[]{100,10,100,1000},-1);
4: /*用Toast显示震动启动*/
5: Toast.makeText(EX05_06.this, getString(R.string.str_ok) ,Toast.LENGTH_SHORT).show();
6: /*取消震动*/
7: mVibrator01.cancel();
8: /*用Toast显示震动取消*/
9: Toast.makeText(EX05_06.this, getString(R.string.str_end) ,Toast.LENGTH_SHORT).show();
10: