Android中振動器(Vibrator)的使用

jopen 11年前發布 | 68K 次閱讀 Android Android開發 移動開發

系統獲取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:

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:
    <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>  
來自:http://blog.csdn.net/u010142437/article/details/9285615

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!