【Android】Vibrator(震动模式)设置长短震动

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();
			}
 
		}
 
	}
}
震动权限:android.permission.VIBRATE

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 是一种手机提醒的方式,可以在接收到消息、来电或者其他事件时通过震动的方式提示用户。在Android中,可以使用Vibrator类来控制手机的震动,需要在AndroidManifest.xml文件中添加震动权限声明。具体的实现可以参考以下代码: ``` // 获取Vibrator实例 Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); // 震动500毫秒 vibrator.vibrate(500); ``` ### 回答2: Android手机震动是一种通过振动马达来产生震动效果的功能。它可以通过调用系统提供的震动API来实现。 Android手机震动功能广泛应用于各种应用程序中,例如来电提醒、消息通知、游戏反馈等。当手机处于静音或振动模式时,来电时手机会通过震动让用户知道有来电;当手机接收到消息或通知时,也会通过震动提醒用户;在游戏中,震动可以增强用户的体验和反馈,例如在打击敌人或取得游戏进步时会触发震动效果。 开发者可以通过使用Android提供的Vibrator类来控制手机的震动功能。首先需要获取到Vibrator实例,然后调用其vibrate方法即可触发震动vibrate方法通常有几种重载形式,可以指定震动持续时间、震动模式等参数。 同时,Android还提供了一些预定义的震动模式,开发者可以通过使用系统提供的常量来实现不同的震动效果,如长震动、短震动、循环震动等。此外,还可以自定义震动模式,通过指定一系列的持续时间和间隔时间来实现特殊的震动效果。 总之,Android手机震动是一项非常实用和丰富的功能,通过它可以给用户带来更好的使用体验,同时也为开发者提供了更多的交互反馈方式。 ### 回答3: Android手机的震动功能是一种可以通过设置来调整的功能,用于在接收到新消息、来电或其他事件时提醒用户。 首先,用户可以在手机的设置菜单中找到“声音和振动”或类似的选项。在这个菜单中,用户可以选择是否允许手机震动。当用户将此设置打开时,手机在特定事件发生时就会震动。 通常情况下,当有新消息、来电或其他通知时,手机会发出声音和震动。但是,用户也可以根据自己的需要进行个性化设置。例如,可以设置仅在静音模式下允许震动,或者禁用声音但保留震动等。 此外,有些应用程序还提供了自定义震动模式选项。用户可以根据自己的喜好和需求,选择不同的震动模式。例如,有的人喜欢强烈的长震动,而有的人则喜欢短促的轻微震动。通过这些设置,用户可以根据自己的喜好来调整震动效果。 总的来说,Android手机的震动功能是为了提醒用户新消息、来电或其他事件的一种方式。通过设置菜单,用户可以自定义是否启用震动、在哪些情况下震动以及震动的效果。这种功能的存在可以让用户在不方便或无法依赖声音提醒时,通过震动来感知到新消息或事件的到来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值