Android中振动器(Vibrator)的使用

系统获取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(); 
        
   
    
}
布局XML:
?
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 >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值