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:

package com.guyun.vibratortest;

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>


 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM