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>


你可能感兴趣的:(android)