Android中振动器(Vibrator)的使用

本文介绍如何在Android设备上使用Vibrator API控制手机振动。详细解释了三种振动方法:指定毫秒数振动、按模式振动及关闭振动。并通过一个示例应用展示了如何实现不同类型的振动效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

系统获取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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值