Android--Vibrator(震动模式)

本文介绍了Android系统的Vibrator API,展示了如何在应用中实现设备的震动效果,并提供了相关代码示例,包括main.xml中的配置和运行时的操作。

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

main.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ToggleButton
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="ON"
        android:textOff="OFF"
        android:text="短震动" />

    <ToggleButton
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="ON"
        android:textOff="OFF"
        android:text="长震动" />

    <ToggleButton
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="ON"
        android:textOff="OFF"
        android:text="节奏震动" />

</LinearLayout>

.java代码如下:

package org.lxh.demo;

import android.app.Activity;
import android.app.Service;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.ToggleButton;

public class Hello extends Activity {
	private ToggleButton btn1 = null;
	private ToggleButton btn2 = null;
	private ToggleButton btn3 = null;
	private Vibrator myVibrator = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); // 生命周期方法
		super.setContentView(R.layout.main); // 设置要使用的布局管理器
		this.myVibrator = (Vibrator) getApplication().getSystemService(
				Service.VIBRATOR_SERVICE);
		this.btn1 = (ToggleButton) super.findViewById(R.id.btn1);
		this.btn2 = (ToggleButton) super.findViewById(R.id.btn2);
		this.btn3 = (ToggleButton) super.findViewById(R.id.btn3);
		this.btn1.setOnClickListener(new Btn1());
		this.btn2.setOnClickListener(new Btn2());
		this.btn3.setOnClickListener(new Btn3());

	}

	private class Btn1 implements OnClickListener {

		public void onClick(View arg0) {
			if (btn1.isChecked()) {
				Hello.this.myVibrator.vibrate(
						new long[] { 100, 10, 100, 1000 }, -1);
				Toast.makeText(Hello.this, "短震动", Toast.LENGTH_SHORT).show();
			} else {
				Hello.this.myVibrator.cancel();
				Toast.makeText(Hello.this, "取消短震动", Toast.LENGTH_SHORT).show();
			}

		}

	}

	private class Btn2 implements OnClickListener {

		public void onClick(View arg0) {
			if (btn2.isChecked()) {
				Hello.this.myVibrator.vibrate(
						new long[] { 100, 100, 100, 1000 }, 0);
				Toast.makeText(Hello.this, "长震动", Toast.LENGTH_SHORT).show();
			} else {
				Hello.this.myVibrator.cancel();
				Toast.makeText(Hello.this, "取消长震动", Toast.LENGTH_SHORT).show();
			}

		}

	}

	private class Btn3 implements OnClickListener {

		public void onClick(View arg0) {
			if (btn3.isChecked()) {
				Hello.this.myVibrator.vibrate(
						new long[] { 1000, 50, 1000, 50 }, 0);
				Toast.makeText(Hello.this, "节奏震动", Toast.LENGTH_SHORT).show();
			} else {
				Hello.this.myVibrator.cancel();
				Toast.makeText(Hello.this, "取消节奏震动", Toast.LENGTH_SHORT).show();
			}

		}

	}
}
配置权限:<uses-permission android:name="android.permission.VIBRATE"/>

运行如下:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值