系统获取Vibrator也是调用Context的getSystemService方法,接下来就可以调用Vibrator的方法控制手机振动了。Vibrator只有三个方法控制手机振动:
1、vibrate(long milliseconds):控制手机振动的毫秒数。
2、vibrate(long[] pattern,int repeat):指定手机以pattern模式振动,例如指定pattern为new long[]{400,800,1200,1600},就是指定在400ms、800ms、1200ms、1600ms这些时间点交替启动、关闭手机振动器,其中repeat指定pattern数组的索引,指定pattern数组中从repeat索引开始的振动进行循环。-1表示只振动一次,非-1表示从 pattern的指定下标开始重复振动。
3、cancel():关闭手机振动
下面通过一个示例来演示Vibrator的使用:
Activity:
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
53
|
import
android.app.Activity;
import
android.os.Bundle;
import
android.os.Vibrator;
import
android.widget.CompoundButton;
import
android.widget.CompoundButton.OnCheckedChangeListener;
import
android.widget.ToggleButton;
public
class
VibratorTestActivity
extends
Activity
implements
OnCheckedChangeListener {
private
Vibrator vibrator;
private
ToggleButton tog1;
private
ToggleButton tog2;
private
ToggleButton tog3;
private
ToggleButton tog4;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_vibrator_test);
// 获取系统的Vibrator服务
vibrator = (Vibrator)
this
.getSystemService(VIBRATOR_SERVICE);
tog1 = (ToggleButton) findViewById(R.id.activity_vibrator_test_tb1);
tog2 = (ToggleButton) findViewById(R.id.activity_vibrator_test_tb2);
tog3 = (ToggleButton) findViewById(R.id.activity_vibrator_test_tb3);
tog4 = (ToggleButton) findViewById(R.id.activity_vibrator_test_tb4);
tog1.setOnCheckedChangeListener(
this
);
tog2.setOnCheckedChangeListener(
this
);
tog3.setOnCheckedChangeListener(
this
);
tog4.setOnCheckedChangeListener(
this
);
}
@Override
public
void
onCheckedChanged(CompoundButton buttonView,
boolean
isChecked) {
if
(isChecked) {
if
(buttonView == tog1) {
// 设置震动周期
vibrator.vibrate(
new
long
[] {
1000
,
10
,
100
,
1000
}, -
1
);
}
else
if
(buttonView == tog2) {
vibrator.vibrate(
new
long
[] {
100
,
100
,
100
,
1000
},
0
);
}
else
if
(buttonView == tog3) {
vibrator.vibrate(
new
long
[] {
1000
,
50
,
1000
,
50
,
1000
},
0
);
}
else
if
(buttonView == tog4) {
// 设置震动时长
vibrator.vibrate(
5000
);
}
}
else
{
// 关闭震动
vibrator.cancel();
}
}
}
|
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:orientation
=
"vertical"
>
<
LinearLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:orientation
=
"horizontal"
>
<
TextView
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"单次震动:"
/>
<
ToggleButton
android:id
=
"@+id/activity_vibrator_test_tb1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:textOff
=
"开启"
android:textOn
=
"关闭"
/>
LinearLayout
>
<
LinearLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:orientation
=
"horizontal"
>
<
TextView
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"持续震动:"
/>
<
ToggleButton
android:id
=
"@+id/activity_vibrator_test_tb2"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:textOff
=
"开启"
android:textOn
=
"关闭"
/>
LinearLayout
>
<
LinearLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:orientation
=
"horizontal"
>
<
TextView
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"节奏震动:"
/>
<
ToggleButton
android:id
=
"@+id/activity_vibrator_test_tb3"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:textOff
=
"开启"
android:textOn
=
"关闭"
/>
LinearLayout
>
<
LinearLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:orientation
=
"horizontal"
>
<
TextView
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"定时长震动:"
/>
<
ToggleButton
android:id
=
"@+id/activity_vibrator_test_tb4"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:textOff
=
"开启"
android:textOn
=
"关闭"
/>
LinearLayout
>
LinearLayout
>
|