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中振动器(Vibrator)的使用的更多相关文章
- Android中图像变换Matrix的原理、代码验证和应用(三)
第三部分 应用 在这一部分,我们会将前面两部分所了解到的内容和Android手势结合起来,利用各种不同的手势对图像进行平移.缩放和旋转,前面两项都是在实践中经常需要用到的功能,后一项据说苹果也是最近才 ...
- 一个Demo学完Android中所有的服务(转)
说明:这个例子实现了Android中常见的许多服务,下面是实现的截图 接下来,以源代码的方式分析这个例子 1.MainActivity--主界面 这个类主要是实现用户所看到的这个Activity, ...
- Andorid手机振动器(Vibrator)的使用
标签: android vibrator 震动器 it 分类: Andorid 获取振动器Vibrator实例: Vibrator mVibrator = (Vibrator) context.ge ...
- Android中的LinearLayout布局
LinearLayout : 线性布局 在一般情况下,当有很多控件需要在一个界面列出来时,我们就可以使用线性布局(LinearLayout)了, 线性布局是按照垂直方向(vertical)或水平方向 ...
- Android中BroadcastReceiver的两种注册方式(静态和动态)详解
今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来 ...
- Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)
之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...
- Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)
昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...
- Android中Fragment和ViewPager那点事儿(仿微信APP)
在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...
- Android中Fragment与Activity之间的交互(两种实现方式)
(未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...
随机推荐
- Kendo UI开发教程(20): Kendo MVVM 数据绑定(九) Text
Text绑定可以使用ViewModel来设置DOM元素的文本属性,如果需要设置input,textarea,或select的显示,需要使用value属性. 1 <span data-bind=& ...
- android布局margin和padding差异!
事实上从使用的时候就能够差别开来. android:padding android:layout_margin padding是在本控件级别的,而margin是在layout级别的. 最好拿有背景的控 ...
- Rationnal Rose2003安装并破解
1.安装Rational Rose2003时,在需选择安装项的时候,只选择Rational Rose EnterPrise Edition即可,不需选择其他项,之后选择“DeskTop Install ...
- PHP之操作数据库
数据库,顾名思义,是一个存放数据的容器.然后在使用过程中对数据库里面的数据增删改查,具体是怎么实现的呢? 这儿不得不提一下一个神奇的东西:SQL语句:结构化查询语言(Structured Query ...
- C语言中 struct成员变量顺序对内存的占用
在C语言的结构体中,是会按照其变量类型来进行分配内存大小的.但是对于不同的编译器,结果是不同的,在VC++6.0中是怎么个分配情况呢?用一下C中的关键字sizeof()来测试下,注意sizeof()不 ...
- 怎样配置PHP环境和安装Zendstdio编辑器
想学习PHP好久了.苦于环境配置不好,一直感觉无从下手. 在网上找了个视频: 李炎恢PHP教程 第一章前3节给出了具体的配置的方法,即安装Apache和Zendstudio 10.5仅仅须要照着视频做 ...
- Cocos2dx中Plugin-X 在android下的整合
直接拉plugin-x中的jar包导入到Eclipse中就可以.用这么麻烦的工具干嘛.
- Swift - 使用NSURL进行数据的提交和获取(POST与GET)
使用Swift进行iOS开发时,不可避免的要进行远程的数据获取和提交. 其数据请求的方式既可能是POST也可能是GET.同不管是POST还是GET又可以分为同步请求和异步请求. 下面通过四个例子来进行 ...
- HDU3714 Error Curves (单峰函数)
大意: 给你n个二次函数Si(x),F(x) = max{Si(x)} 求F(x)在[0,1000]上的最小值. S(x)=ax^2+bx+c (0<=a<=100, |b|, ...
- linux命名管道通信过程
前一个道,这节学习命名管道. 二命名管道 无名管道仅仅能用来在父子进程或兄弟进程之间进行通信,这就给没有亲缘关系的进程之间数据的交换带来了麻烦.解决问题就是本节要学习的还有一种管道通信:命名管道. 命 ...