Android-Vibrator的使用


Android手机中的震动由Vibrator实现。设置震动事件,需要知道其震动的时间长短、震动的周期等。

在android中,震动的时间一毫秒计算(1/1000秒),所以如果设置的时间值太小,会感觉不出来。

通过调用Vibrator的vibrate(long[] pattern, int repeat)方法实现。

前一个参数为设置震动的效果的数组,第二个参数为 -1表示只震动一次,为0则震动会一直持续。

一个demo:

  1. package com.shao.vibrator;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.os.Vibrator;  
  6. import android.widget.CompoundButton;  
  7. import android.widget.Toast;  
  8. import android.widget.CompoundButton.OnCheckedChangeListener;  
  9. import android.widget.ToggleButton;  
  10.   
  11. public class VibratorActivity extends Activity {  
  12.     /** Called when the activity is first created. */  
  13.   
  14.     private Vibrator vibrator;   
  15.     private  ToggleButton  tog1;  
  16.     private  ToggleButton  tog2;  
  17.     private  ToggleButton  tog3;  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         init();  
  23.         tog1.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  24.               
  25.             @Override  
  26.             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  27.                 // TODO Auto-generated method stub   
  28.                 if(isChecked){  
  29.                     //设置震动周期   
  30.                     vibrator.vibrate(new long[]{1000,10,100,1000}, -1);  
  31.                     showToast("OK");  
  32.                 }else{  
  33.                     //取消震动   
  34.                     vibrator.cancel();  
  35.                     showToast("CANCEL");  
  36.                 }  
  37.             }  
  38.         });  
  39.  tog2.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  40.               
  41.             @Override  
  42.             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  43.                 // TODO Auto-generated method stub   
  44.                 if(isChecked){  
  45.                     //设置震动周期   
  46.                     vibrator.vibrate(new long[]{100,100,100,1000}, 0);  
  47.                     showToast("OK");  
  48.                 }else{  
  49.                     //取消震动   
  50.                     vibrator.cancel();  
  51.                     showToast("CANCEL");  
  52.                 }  
  53.             }  
  54.         });  
  55.  tog3.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  56.           
  57.         @Override  
  58.         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  59.             // TODO Auto-generated method stub   
  60.             if(isChecked){  
  61.                 //设置震动周期   
  62.                 vibrator.vibrate(new long[]{1000,50,1000,50,1000}, 0);  
  63.                 showToast("OK");  
  64.             }else{  
  65.                 //取消震动   
  66.                 vibrator.cancel();  
  67.                 showToast("CANCEL");  
  68.             }  
  69.         }  
  70.     });  
  71.           
  72.     }  
  73.     private void  init(){  
  74.         tog1= (ToggleButton) findViewById(R.id.tog1);  
  75.         tog2= (ToggleButton) findViewById(R.id.tog2);  
  76.         tog3= (ToggleButton) findViewById(R.id.tog3);  
  77.         vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);  
  78.     }  
  79.     private void showToast(String msg){  
  80.         Toast.makeText(this, msg, 1).show();  
  81.     }  
  82. }  
xml:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <RelativeLayout   
  8.       android:layout_marginTop="20dp"  
  9.     android:orientation="horizontal"  
  10.     android:layout_width="fill_parent"  
  11.     android:layout_height="wrap_content">  
  12.     <TextView    
  13.     android:layout_width="wrap_content"   
  14.     android:layout_height="wrap_content"   
  15.        android:text="短震动"  
  16.     />  
  17. <ToggleButton   
  18.     android:id="@+id/tog1"  
  19.     android:layout_width="wrap_content"   
  20.     android:layout_height="wrap_content"   
  21.     android:textOn="关闭"  
  22.     android:textOff="打开"  
  23.     android:layout_alignParentRight="true"  
  24.     />  
  25.   </RelativeLayout>   
  26.     <RelativeLayout   
  27.       android:layout_marginTop="20dp"  
  28.     android:orientation="horizontal"  
  29.     android:layout_width="fill_parent"  
  30.     android:layout_height="wrap_content">  
  31.     <TextView    
  32.     android:layout_width="wrap_content"   
  33.     android:layout_height="wrap_content"   
  34.   
  35.      android:text="长震动"  
  36.     />  
  37. <ToggleButton    
  38.     android:id="@+id/tog2"  
  39.     android:layout_width="wrap_content"   
  40.     android:layout_height="wrap_content"   
  41.      android:textOn="关闭"  
  42.     android:textOff="打开"  
  43.     android:layout_alignParentRight="true"  
  44.     />  
  45.   </RelativeLayout>   
  46.     <RelativeLayout   
  47.       android:layout_marginTop="20dp"  
  48.     android:orientation="horizontal"  
  49.     android:layout_width="fill_parent"  
  50.     android:layout_height="wrap_content">  
  51.     <TextView    
  52.     android:layout_width="wrap_content"   
  53.     android:layout_height="wrap_content"   
  54.   
  55.      android:text="节奏震动"  
  56.     />  
  57. <ToggleButton    
  58.     android:id="@+id/tog3"  
  59.     android:layout_width="wrap_content"   
  60.     android:layout_height="wrap_content"   
  61.      android:textOn="关闭"  
  62.     android:textOff="打开"  
  63.     android:layout_alignParentRight="true"  
  64.     />  
  65.   </RelativeLayout>   
  66. </LinearLayout>  

最后别忘了加上    <uses-permission  android:name="android.permission.VIBRATE"/> 权限

相关内容