Vibrator控制手机震动

效果图

源码

下载地址(Android Studio工程):http://download.csdn.net/detail/q4878802/9049755

添加权限

<!-- 震动的权限 -->
<uses-permission android:name="android.permission.VIBRATE" />

工具类

package com.kongqw.kqwvibrator.engine;

import android.content.Context;
import android.os.Vibrator; /**
* Created by kongqw on 2015/8/26.
*/
public class KqwVibrator { private static KqwVibrator mKqwVibrator;
private Context mContext;
private Vibrator mVibrator; private KqwVibrator(Context context) {
mContext = context;
// 初始化振动器对象
mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
} public static KqwVibrator getInstance(Context context) {
if (null == mKqwVibrator) {
mKqwVibrator = new KqwVibrator(context);
}
return mKqwVibrator;
} /**
* 开始震动
*
* @param time 震动时间 毫秒
*/
public void vibrate(long time) {
mVibrator.vibrate(time);
} /**
* 取消震动
*/
public void cancle() {
mVibrator.cancel();
} /**
* 重复震动
*/
public void repeatVibrate() {
//等待1秒,震动2秒,等待1秒,震动3秒
long[] pattern = {1000, 2000, 1000, 3000};
//-1表示不重复, 如果不是-1, 比如改成1, 表示从前面这个long数组的下标为1的元素开始重复.
mVibrator.vibrate(pattern, -1);
}
}

测试类

package com.kongqw.kqwvibrator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View; import com.kongqw.kqwvibrator.engine.KqwVibrator; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} /**
* 开始震动500毫秒
*
* @param view
*/
public void button1(View view) {
KqwVibrator.getInstance(this).vibrate(500);
} /**
* 重复震动
*
* @param view
*/
public void button2(View view) {
KqwVibrator.getInstance(this).repeatVibrate();
} /**
* 停止震动
*
* @param view
*/
public void button3(View view) {
KqwVibrator.getInstance(this).cancle();
}
}

页面布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"> <Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:onClick="button1"
android:text="开始震动500毫秒" /> <Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:onClick="button2"
android:text="重复震动" /> <Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/button2"
android:onClick="button3"
android:text="停止震动" /> </RelativeLayout>

说明

mVibrator.vibrate(pattern, -1);

这个是一个间歇性震动的方法,第一个参数是一个long类型的数组(毫秒),单数(双数角标)为等待时间,双数(单数角标)为震动时间。

例:

//等待1秒,震动2秒,等待1秒,震动3秒
long[] pattern = {1000, 2000, 1000, 3000};

第二个参数是循环的参数,是几就表示下次从第几个角标开始循环,-1表示不循环。

父类方法

/**
* Vibrate with a given pattern.
*
* <p>
* Pass in an array of ints that are the durations for which to turn on or off
* the vibrator in milliseconds. The first value indicates the number of milliseconds
* to wait before turning the vibrator on. The next value indicates the number of milliseconds
* for which to keep the vibrator on before turning it off. Subsequent values alternate
* between durations in milliseconds to turn the vibrator off or to turn the vibrator on.
* </p><p>
* To cause the pattern to repeat, pass the index into the pattern array at which
* to start the repeat, or -1 to disable repeating.
* </p>
* <p>This method requires the caller to hold the permission
* {@link android.Manifest.permission#VIBRATE}.
*
* @param pattern an array of longs of times for which to turn the vibrator on or off.
* @param repeat the index into pattern at which to repeat, or -1 if
* you don't want to repeat.
*/
public void vibrate(long[] pattern, int repeat) {
vibrate(pattern, repeat, null);
} /**
* Vibrate with a given pattern.
*
* <p>
* Pass in an array of ints that are the durations for which to turn on or off
* the vibrator in milliseconds. The first value indicates the number of milliseconds
* to wait before turning the vibrator on. The next value indicates the number of milliseconds
* for which to keep the vibrator on before turning it off. Subsequent values alternate
* between durations in milliseconds to turn the vibrator off or to turn the vibrator on.
* </p><p>
* To cause the pattern to repeat, pass the index into the pattern array at which
* to start the repeat, or -1 to disable repeating.
* </p>
* <p>This method requires the caller to hold the permission
* {@link android.Manifest.permission#VIBRATE}.
*
* @param pattern an array of longs of times for which to turn the vibrator on or off.
* @param repeat the index into pattern at which to repeat, or -1 if
* you don't want to repeat.
* @param attributes {@link AudioAttributes} corresponding to the vibration. For example,
* specify {@link AudioAttributes#USAGE_ALARM} for alarm vibrations or
* {@link AudioAttributes#USAGE_NOTIFICATION_RINGTONE} for
* vibrations associated with incoming calls.
*/
public void vibrate(long[] pattern, int repeat, AudioAttributes attributes) {
vibrate(Process.myUid(), mPackageName, pattern, repeat, attributes);
}

Vibrator控制手机震动的更多相关文章

  1. js控制手机震动

    js控制手机震动 <button onclick="vibrate()">震动</button> <script> function vibra ...

  2. 使用Vibrator控制手机振动

    import android.os.Bundle;import android.os.Vibrator;import android.app.Activity;import android.app.S ...

  3. Java-Android【1】-控制手机震动

    一.配置震动授权 1.在AndroidManifest.xml文件中添加<manifest></manifest>中添加一行 <uses-permission andro ...

  4. cocos2d-x3.2中加入Android手机震动

    本人宣布从此博文发出后,我的cocos2dx的引擎从cocos2dx3.1.1跳到cocos2dx3.2,哈哈,事实上变化不大的,不碍事~~~ 以下来说说在cocos中加入Android手机震动的功能 ...

  5. [android] 手机卫士输入框抖动和手机震动

    查看apiDemos,找到View/Animation/shake找到对应的动画代码,直接拷贝过来 当导入一个项目的时候,报R文件不存在,很多情况是xml文件出错了 Animation shake = ...

  6. 【Android】Android如何实现手机震动

    实现手机震动其实很简单,手机震动使用是Vibrator类,然后震动也是需要权限的,在使用之前在AndroidManifest.xml文件中添加 <uses-permission android: ...

  7. Android 手机震动 设置震动时间

    开启震动,单次,5秒: Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); //震动5秒 vibrator.vibra ...

  8. 在Airtest中如何使用无线模式控制手机

    在使用Airtest超快速开发App爬虫文章的最后,我们留了一个尾巴:如何启动Airtest的无线模式,不用USB线就能控制手机? 本文将会讲到具体的做法.做法分为两种:第一种是在Airtest的ID ...

  9. JS 判断浏览器类型,获取位置信息,让手机震动

    判断是否是安卓 var isAndroid = /Android/i.test(navigator.userAgent); 判断是否是IOS系统 var isIOS = /iPhone|iPad|iP ...

随机推荐

  1. [Luogu 1559]运动员最佳匹配问题

    Description 题库链接 求 \(2\times N\) 个点的带权二分图最佳匹配. \(1\leq N\leq 20\) Solution 我还是太菜了啊...到现在才学 \(KM\) . ...

  2. [AHOI 2012]树屋阶梯

    Description 暑假期间,小龙报名了一个模拟野外生存作战训练班来锻炼体魄,训练的第一个晚上,教官就给他们出了个难题.由于地上露营湿气重,必须选择在高处的树屋露营.小龙分配的树屋建立在一颗高度为 ...

  3. [HNOI2015]落忆枫音

    题目描述 「恒逸,你相信灵魂的存在吗?」 郭恒逸和姚枫茜漫步在枫音乡的街道上.望着漫天飞舞的红枫,枫茜突然问出这样一个问题. 「相信吧.不然我们是什么,一团肉吗?要不是有灵魂......我们也不可能再 ...

  4. [HAOI2009]逆序对数列

    题目描述 对于一个数列{ai},如果有i<j且ai>aj,那么我们称ai与aj为一对逆序对数.若对于任意一个由1~n自然数组成的数列,可以很容易求出有多少个逆序对数.那么逆序对数为k的这样 ...

  5. 【BZOJ2186】【SDOI2008】沙拉公主的困惑

    Description ​ 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行编号与M!互质的钞票.房地产第一大户沙拉公主决定预测一下大富翁 ...

  6. 【BZOJ1060】【ZJOI2007】时态同步

    Description 小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字1,2,3-.进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路板 ...

  7. hdu 5750 Dertouzos 素数

    Dertouzos Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  8. [ZJOI2006]物流运输 SPFA+DP

    题目描述 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格的管理和跟踪. ...

  9. 51 nod 1188 最大公约数之和 V2

    1188 最大公约数之和 V2 题目来源: UVA 基准时间限制:2 秒 空间限制:262144 KB 分值: 160 难度:6级算法题   给出一个数N,输出小于等于N的所有数,两两之间的最大公约数 ...

  10. poj 2318 叉积+二分

    TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13262   Accepted: 6412 Description ...