系统获取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中振动器(Vibrator)的使用的更多相关文章

  1. Android中图像变换Matrix的原理、代码验证和应用(三)

    第三部分 应用 在这一部分,我们会将前面两部分所了解到的内容和Android手势结合起来,利用各种不同的手势对图像进行平移.缩放和旋转,前面两项都是在实践中经常需要用到的功能,后一项据说苹果也是最近才 ...

  2. 一个Demo学完Android中所有的服务(转)

    说明:这个例子实现了Android中常见的许多服务,下面是实现的截图 接下来,以源代码的方式分析这个例子   1.MainActivity--主界面 这个类主要是实现用户所看到的这个Activity, ...

  3. Andorid手机振动器(Vibrator)的使用

    标签: android vibrator 震动器 it 分类: Andorid 获取振动器Vibrator实例: Vibrator  mVibrator = (Vibrator) context.ge ...

  4. Android中的LinearLayout布局

    LinearLayout : 线性布局 在一般情况下,当有很多控件需要在一个界面列出来时,我们就可以使用线性布局(LinearLayout)了,  线性布局是按照垂直方向(vertical)或水平方向 ...

  5. Android中BroadcastReceiver的两种注册方式(静态和动态)详解

    今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来 ...

  6. Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)

    之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...

  7. Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)

    昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...

  8. Android中Fragment和ViewPager那点事儿(仿微信APP)

    在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...

  9. Android中Fragment与Activity之间的交互(两种实现方式)

    (未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...

随机推荐

  1. Window7下安装openssl完整版(亲测实现)

    安装环境: 操作系统:window7(64位) C++编译器:VS2010 -------------------------------------------------------------- ...

  2. zabbix 监控jmx 需要--enable-java

    安装Javagateway如果原来已经安装zabbix,只需要再添加以下zabbix-java # tar zxvf zabbix-2.2.0.tar.gz # cd zabbix-2.2.0 # . ...

  3. 最完整的历史记录hadoop

    课程主要涉及Hadoop Sqoop.Flume.Avro重要子项目的技术实战 课程针对人群 1.本课程适合于有一定java基础知识.对数据库和sql语句有一定了解,熟练使用linux系统的技术人员, ...

  4. 如果是在有master上开启了该参数,记得在slave端也要开启这个参数(salve需要stop后再重新start),否则在master上创建函数会导致replaction中断。

    如果是在有master上开启了该参数,记得在slave端也要开启这个参数(salve需要stop后再重新start),否则在master上创建函数会导致replaction中断.

  5. 使用iftop网络流量监控

    iftop这是一个非常有用的工具.下面的命令监视无线网卡在我的笔记本 iftop -i wlan0 比如,我现在玩音乐视频.iftop显示的信息: 基本说明: 1. 屏幕主要部分都是表示两个机器之间的 ...

  6. Maven的生命周期

    每次读.每次忘,Mark一下以后忘记就不翻书了! Maven有三套相互独立的生命周期,各自是:clean.default.site. clean主要是清理项目. default是Maven最核心的的构 ...

  7. HDU4911-Inversion(树状数组)

    Inversion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Tota ...

  8. Ubuntu升级到14.04

    公司网络实在太翔了,搞了一天最终成功把ubuntu从13.10升级到了14.10,中间也越到了非常多问题,记录下来,以备參考. 13.10的时候想体验一把搜狗输入法,结果因为fcitx版本号太低,用了 ...

  9. Redis Destop Manager不能访问虚拟机

    虚拟机centOS中安装Redis,主机Redis Destop Manager不能访问虚拟机Redis server的解决方案 今天在学些redis的时候碰到个问题,发现主机Redis Destop ...

  10. ThinkPhp学习08

    原文:ThinkPhp学习08 一.普通查询方式 a.字符串 $arr=$m->where("sex=0 and username='gege'")->find(); ...