Vibrator控制手机震动
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控制手机震动的更多相关文章
- js控制手机震动
js控制手机震动 <button onclick="vibrate()">震动</button> <script> function vibra ...
- 使用Vibrator控制手机振动
import android.os.Bundle;import android.os.Vibrator;import android.app.Activity;import android.app.S ...
- Java-Android【1】-控制手机震动
一.配置震动授权 1.在AndroidManifest.xml文件中添加<manifest></manifest>中添加一行 <uses-permission andro ...
- cocos2d-x3.2中加入Android手机震动
本人宣布从此博文发出后,我的cocos2dx的引擎从cocos2dx3.1.1跳到cocos2dx3.2,哈哈,事实上变化不大的,不碍事~~~ 以下来说说在cocos中加入Android手机震动的功能 ...
- [android] 手机卫士输入框抖动和手机震动
查看apiDemos,找到View/Animation/shake找到对应的动画代码,直接拷贝过来 当导入一个项目的时候,报R文件不存在,很多情况是xml文件出错了 Animation shake = ...
- 【Android】Android如何实现手机震动
实现手机震动其实很简单,手机震动使用是Vibrator类,然后震动也是需要权限的,在使用之前在AndroidManifest.xml文件中添加 <uses-permission android: ...
- Android 手机震动 设置震动时间
开启震动,单次,5秒: Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); //震动5秒 vibrator.vibra ...
- 在Airtest中如何使用无线模式控制手机
在使用Airtest超快速开发App爬虫文章的最后,我们留了一个尾巴:如何启动Airtest的无线模式,不用USB线就能控制手机? 本文将会讲到具体的做法.做法分为两种:第一种是在Airtest的ID ...
- JS 判断浏览器类型,获取位置信息,让手机震动
判断是否是安卓 var isAndroid = /Android/i.test(navigator.userAgent); 判断是否是IOS系统 var isIOS = /iPhone|iPad|iP ...
随机推荐
- JavaScript根据经纬度获取距离信息
最近开发微信小程序,遇到了外卖配送半径的问题,在网上查阅了诸多资料,也大概理解了经纬度距离计算的公式原理,在此做下笔记,方便自己和大家学习使用. 若是把地球当作一个正常的球体(其实它是椭球)来说,球面 ...
- [WC 2006]水管局长数据加强版
Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...
- [APIO2016]划艇
题目描述 在首尔城中,汉江横贯东西.在汉江的北岸,从西向东星星点点地分布着 NNN 个划艇学校,编号依次为 111 到 NNN.每个学校都拥有若干艘划艇.同一所学校的所有划艇颜色相同,不同的学校的划艇 ...
- [SDOI2009]学校食堂Dining
题目描述 小F 的学校在城市的一个偏僻角落,所有学生都只好在学校吃饭.学校有一个食堂,虽然简陋,但食堂大厨总能做出让同学们满意的菜肴.当然,不同的人口味也不一定相同,但每个人的口味都可以用一个非负整数 ...
- AtCoder Grand Contest 002 D - Stamp Rally
Description We have an undirected graph with N vertices and M edges. The vertices are numbered 1 thr ...
- poj 3348 Cow 凸包面积
Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8122 Accepted: 3674 Description ...
- 【Codeforces Round #430 (Div. 2) A C D三个题】
·不论难度,A,C,D自己都有收获! [A. Kirill And The Game] ·全是英文题,述大意: 给出两组区间端点:l,r,x,y和一个k.(都是正整数,保证区间不为空),询问是否 ...
- [Helvetic Coding Contest 2017 online mirror]
来自FallDream的博客,未经允许,请勿转载,谢谢, 第一次在cf上打acm...和同校大佬组队打 总共15题,比较鬼畜,最后勉强过了10题. AB一样的题目,不同数据范围,一起讲吧 你有一个背包 ...
- Selenium之unittest测试框架详谈及实操
申明:本文是基于python3.x及selenium3.x. unittest,也可以称为PyUnit,可以用来创建全面的测试套件,可以用于单元自动化测试(模块).功能自动化测试(UI)等等. 官方文 ...
- 360面试-C++后端(实习)
在线远程视频面试 一面: 自我介绍. 知道哪几种排序算法,各算法的时间复杂度. 解决hash冲突的几种方式. 有哪些方法清除cache中旧的数据.不太清楚,我扯到了操作系统中缺页中断的页面置换原理上, ...