Android 手机震动功能实现
在与用户交互时,常常会用到震动功能,以提醒用户。该功能实现比较简单,请参阅下面主要代码:
[java] import android.app.Activity;
import android.app.Service;
import android.os.Vibrator;
public class TipHelper {
public static void Vibrate(final Activity activity, long milliseconds) {
Vibrator vib = (Vibrator) activity.getSystemService(Service.VIBRATOR_SERVICE);
vib.vibrate(milliseconds);
}
public static void Vibrate(final Activity activity, long[] pattern,boolean isRepeat) {
Vibrator vib = (Vibrator) activity.getSystemService(Service.VIBRATOR_SERVICE);
vib.vibrate(pattern, isRepeat ? 1 : -1);
}
}
import android.app.Activity;
import android.app.Service;
import android.os.Vibrator;
public class TipHelper {
public static void Vibrate(final Activity activity, long milliseconds) {
Vibrator vib = (Vibrator) activity.getSystemService(Service.VIBRATOR_SERVICE);
vib.vibrate(milliseconds);
}
public static void Vibrate(final Activity activity, long[] pattern,boolean isRepeat) {
Vibrator vib = (Vibrator) activity.getSystemService(Service.VIBRATOR_SERVICE);
vib.vibrate(pattern, isRepeat ? 1 : -1);
}
}
当然,www.zzzyk.com 仅仅有上面的代码,还是不行的。我们还需要在AndroidManifest.xml 中添加震动权限:
[html] <uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.VIBRATE" />
通过上面操作,我们可以使用TipHelper所定义的函数了。两个Vibrate函数的参数简单介绍如下:
final Activity activity :调用该方法的Activity实例
long milliseconds :震动的时长,单位是毫秒
long[] pattern :自定义震动模式 。数组中数字的含义依次是[静止时长,震动时长,静止时长,震动时长。。。]时长的单位是毫秒
boolean isRepeat : 是否反复震动,如果是true,反复震动,如果是false,只震动一次
摘自 心灵净土
补充:移动开发 , Android ,