添加权限

在AndroidManifest.xml文件中添加如下震动权限

<uses-permission android:name="android.permission.VIBRATE"/> 

获取Vibrator

vibrator = (Vibrator)context.getSystemService(this.VIBRATOR_SERVICE);

简单震动

先看下简单震动方法vibrate(long milliseconds)的源码

/*** Vibrate constantly for the specified period of time.** @param milliseconds The number of milliseconds to vibrate.** @deprecated Use {@link #vibrate(VibrationEffect)} instead.*/
@Deprecated
@RequiresPermission(android.Manifest.permission.VIBRATE)
public void vibrate(long milliseconds) {vibrate(milliseconds, null);
}

使用方法很简单,就是传入一个毫秒值,手机会持续震动该毫秒值时长,震动完即停止

vibrator = (Vibrator)context.getSystemService(this.VIBRATOR_SERVICE);
vibrator.vibrate(1000); // 震动一秒

复杂震动

先看下复杂震动方法vibrate(long[] pattern, int repeat)的源码及注释说明

/*** 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>** @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.** @deprecated Use {@link #vibrate(VibrationEffect)} instead.*/
@Deprecated
@RequiresPermission(android.Manifest.permission.VIBRATE)
public void vibrate(long[] pattern, int repeat) {vibrate(pattern, repeat, null);
}
  1. 第一个参数pattern表示的是震动的模式,是一个long数组,表示的是震动和停止震动交替的时长,且第一个为停止震动的时长,第二个为震动的时长,第三个为停止震动的时长,以此类推。
  2. 第二个参数repeat表示从pattern中的第几个位置开始无限重复震动,若设置成-1则表示不重复震动
vibrator = (Vibrator)context.getSystemService(this.VIBRATOR_SERVICE);
long[] pattern = {100, 200, 100, 200}; // 100ms后开始震动200ms,然后再停止100ms,再震动200ms
vibrator.vibrate(pattern, 0); // 从pattern的0位置重复循环震动

若设置成重复震动需调用取消方法停止循环震动

vibrator.cancel();

Android Vibrator手机震动详解相关推荐

  1. 华为手机文件夹android,安卓手机文件目录详解

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 \system\media 铃声音乐文件夹,除了常规的铃声外还有一些系统提示事件音 \system\media\audio \system\media\a ...

  2. android 手机震动1次,Android中手机震动的设置(Vibrator)的步骤

    Android中手机震动的设置(Vibrator)的步骤: a.通过系统服务获得手机震动服务,Vibrator vibrator = (Vibrator)getSystemService(VIBRAT ...

  3. linux 蓝牙 手机遥控器,嵌入式Android小项目之万能手机遥控器详解

    原标题:嵌入式Android小项目之万能手机遥控器详解 在很久很久以前,手机是有红外功能的,后来随着蓝牙技术的成熟,红外逐渐被蓝牙取代,不再是标配了. 红外本身还是有些优点,比如操作简便,成本低.要想 ...

  4. android设置手机震动强度,android 控制震动强度

    转载文章请注明出处:http://blog.csdn.net/dangxw_/article/details/17957175 android 控制手机震动的类为Vibrator 他所提供的方法只有四 ...

  5. Android应用坐标系统全面详解

    Android应用坐标系统全面详解 原文链接:CSDN@工匠若水,http://blog.csdn.net/yanbober/article/details/50419117 1. 背景 去年有很多人 ...

  6. Android开发入门一之Android应用程序架构详解

    Android应用程序架构详解如下: src/ java源代码存放目录 gen/自动生成目录 gen 目录中存放所有由Android开发工具自动生成的文件.目录中最重要的就是R.java文件.这个文件 ...

  7. Android 系统(200)---Android build.prop参数详解

    Android build.prop参数详解 前言 build.prop是Android系统中的一个重要的属性文件,它记录了Android系统运行的很多配置信息,当程序运行时需要某种系统状态时,会到该 ...

  8. Android 系统(95)---Android build.prop参数详解

    Android build.prop参数详解 前言 build.prop是Android系统中的一个重要的属性文件,它记录了Android系统运行的很多配置信息,当程序运行时需要某种系统状态时,会到该 ...

  9. Android的手机震动

    android实现手机震动 1.在AndroidManifest.xml中添加震动权限 <?xml version="1.0" encoding="utf-8&qu ...

最新文章

  1. a = 3 中3在栈还是堆_56.堆、栈、方法区(3.0)
  2. Spring注解方式配置切面类
  3. poj 3487 zoj 1576 稳定婚姻
  4. 代码的简单就在于——直接能看懂
  5. NMS 原理和c++实现,已测试通过
  6. 微信小程序圆形图片小图标按钮
  7. 正则表达式学习实践心得——来源于codesheep(羊哥)
  8. 解决问题multiply defined
  9. netty框架中 bossGroup与workGroup是一个还是两个Reactor
  10. 无线鼠标vs蓝牙鼠标
  11. 阿祥Python自学笔记
  12. 每日新闻:麦肯锡:未来10年 人工智能为全球GDP贡献1.2%增长;工信部下发第19批CDN、云服务牌照...
  13. 剑指Offe(简单)
  14. 计算机地理绘图软件叫什么,地理教师如何选择理想的绘图软件 ──基于对常用绘图软件的比较与分析...
  15. 2009年经典语录雷人总汇
  16. 幂乘法求最大特征值和特征向量
  17. explain的使用
  18. WinRAR如何进行分卷压缩
  19. 前百度首席科学家张栋:36岁以前做到这8点再谈梦想
  20. python编辑word书签_小伙发现了Python中编写word文档的库,编辑文字方便多了

热门文章

  1. CloudXNS试用
  2. element ui 对话框改 模仿苹果手机 预览视图
  3. 什么是专家系统?专家系统的一般结构是什么?
  4. 培养终身阅读习惯的12种方法
  5. 板栗去皮机设计(板栗去壳机设计)【说明书(论文)+ 任务书 +CAD图纸+ SW三维图 + 外文翻译】
  6. Excel当中SUMPRODUCT函数
  7. LJM-II漏电脉冲继电器
  8. PDF文件在线删除水印的方法步骤
  9. raw服务器镜像文件怎么打开,利用qemu-img工具将其它格式镜像文件转换成VHD或RAW格式的方法...
  10. 基于 Pytorch 疾病图片诊断识别 ResNet