haijiang
2018-06-25 586f13d3aa93fc3fdfed65021b1a17a17acf3321
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.moral.andbrickslib.utils;
 
import android.content.Context;
import android.os.Vibrator;
 
/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/9/29
 *     desc  : 震动相关工具类
 * </pre>
 */
public class VibrationUtils {
 
    private VibrationUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }
 
    /**
     * 震动
     * <p>需添加权限 {@code <uses-permission android:name="android.permission.VIBRATE"/>}</p>
     *
     * @param context      上下文
     * @param milliseconds 振动时长
     */
    public static void vibrate(Context context, long milliseconds) {
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(milliseconds);
    }
 
    /**
     * 指定手机以pattern模式振动
     *
     * @param context
     * @param pattern new long[]{400,800,1200,1600},就是指定在400ms、800ms、1200ms、1600ms这些时间点交替启动、关闭手机振动器
     * @param repeat  指定pattern数组的索引,指定pattern数组中从repeat索引开始的振动进行循环。-1表示只振动一次,非-1表示从 pattern的指定下标开始重复振动。
     */
    public static void vibrate(Context context, long[] pattern, int repeat) {
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(pattern, repeat);
    }
 
    /**
     * 取消振动
     *
     * @param context 上下文
     */
    public static void cancel(Context context) {
        ((Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE)).cancel();
    }
}