设置Android系统的震动,需要Vibrator类。
Vibrator类中主要用到两个方法:
public abstract void vibrate(long milliseconds);
public abstract void vibrate(long[] pattern, int repeat);
public abstract void cancel();
<uses-permission android:name="android.permission.VIBRATE" />
<span style="font-family:SimSun;font-size:14px;">import android.os.Vibrator;
/**
* long milliseconds :震动的时长,单位是毫秒
* long[] pattern :自定义震动模式 。数组中数字的含义依次是[静止时长,震动时长,静止时长,震动时长。。。]时长的单位是毫秒
* int repeat :为了重复的按设定的节奏震动,传递参数非-1一直重复振动,用-1表示不重复。
*/
Vibrator vib = (Vibrator) activity.getSystemService(Service.VIBRATOR_SERVICE);
vib.vibrate(milliseconds);
vib.vibrate(pattern, repeat);
vib.cancel(); //取消振动,不然会一直振动下去</span>
SDK中有这样一句话:If your process exits, any vibration you started with will stop. Vibrator类用来操作设备上的震动,如果你的线程退出了,那么启动的震动也会停止