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的两种创建方式>,就如 ...
随机推荐
- 解决VTune错误.../lib64/libstdc++.so.6: version `GLIBCXX_3.4.14' not found (required by ...)
错误信息及出现情景: 在export环境变量LD_PRELOAD=$XTERN_ROOT/dync_hook/interpose.so后,再执行amplxe-gui,出现上述错误.新增的动态链接库对V ...
- 给线程发送消息让它执行不同的处理(自己建立消息循环,非常有意思) good
unit Unit2; interface usesSystem.Classes, Windows, Messages; constWM_DO = WM_USER + 1; typeTDemoThre ...
- C#用正则表达式去掉Html中的script脚本和html标签
原文 C#用正则表达式去掉Html中的script脚本和html标签 /// <summary> /// 用正则表达式去掉Html中的script脚本和html标签 ...
- 【ASP.NET Web API教程】2.1 创建支持CRUD操作的Web API
原文 [ASP.NET Web API教程]2.1 创建支持CRUD操作的Web API 2.1 Creating a Web API that Supports CRUD Operations2.1 ...
- hdu 1387 Team Queue (链表)
题目大意: 不同的人在不同的队伍里,插入链表的时候假设这个链表里有他的队友,就把它放到最后一个队友的最后.假设没有队友,就把它放到整个链表的最后面. 出链表的时候把第一个人拿出来. 思路分析: 要模拟 ...
- Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind
Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind at ...
- “ASP.default_aspx”并不包括“DropDownList1_SelectedIndexChanged”的定义,其解决方法。
"ASP.default_aspx"并不包括"DropDownList1_SelectedIndexChanged"的定义,其解决方法. 在使用DropDown ...
- Delphi xe7并行编程快速入门(三篇)
现在多数设备.计算机都有多个CPU单元,即使是手机也是多核的.但要在开发中使用多核的优势,却需要一些技巧,花费时间编写额外的代码.好了,现在可以使用Delphi做并行编程了. 在Delphi.C++ ...
- CentOS下yum使用代理的设置
export后好像没用? 问题描述: CentOS yum时出现“Could not retrieve mirrorlist http://mirrorlist.centos.org/?release ...
- 开源yYmVc项目 v 0.2 版本号介绍
项目地址:https://code.csdn.net/hacke2/yymvc 本版本号主要实现下面几点功能: 1.框架入口基于过滤器统一实现,action后缀动态配置 2.action配置模仿str ...