1、权限:<uses-permission android:name="android.permission.VIBRATE" />
2、实现震动
//BaseConstantValue.VIBRATE_TIM 震动时间
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
vibrator = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);
long[] pattern = {BaseConstantValue.VIBRATE_TIME, BaseConstantValue.VIBRATE_TIME};
vibrator.vibrate(pattern, -1);
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
vibrator = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);
vibrator.vibrate(BaseConstantValue.VIBRATE_TIME);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);
VibrationEffect vibrationEffect = VibrationEffect.createOneShot(BaseConstantValue.VIBRATE_TIME, VibrationEffect.DEFAULT_AMPLITUDE);
vibrator.vibrate(vibrationEffect);
}
3、释放资源:vibrator.cancel();