前言

Android开启振动主要运用了Vibrator(振动器),系统中有一个Vibrator抽象类,我们可以通过获取Vibrator实例调用里面的方法来完成振动功能。

Vibrator vibrator = (Vibrator) getSystemServic(Service.VIBRATOR_SERVICE);

记得加权限

方法和参数

vibrator.vibrate(1000);  //设置手机振动vibrator.hasVibrator();  //判断手机硬件是否有振动器vibrator.cancel();//关闭振动

这里主要讲解一下vibrator.vibrate(),如下图所示:

vibrate ( long milliseconds )

vibrator.vibrate(1000); //立刻振动,持续时间为1s

vibrate ( long milliseconds, AudioAttributes attributes )

Api文档中对第二个参数的解释是:

attributes: AudioAttributes corresponding to the vibration. For example,specify USAGE_ALARM for alarm vibrations or USAGE_NOTIFICATION_RINGTONE for vibrationsassociated with incoming calls.意思就是说我们可以指定振动对应的属性指定 USAGE_NOTIFICATION_RINGTONE 则是来电铃声振动USAGE_ALARM  闹钟振动

使用

//api 21加入的AudioAttributes audioAttributes = new AudioAttributes.Builder()        .setUsage(AudioAttributes.USAGE_ALARM).build();vibrator.vibrate(1000, audioAttributes);

vibrate ( long [ ] pattern , int repeat )

pattern long类型数组API解释:an array of longs of times for  which to turn the vibrator on or off;官方文档对pattern中元素的详细说明: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.大致意思:数组中的整数用来打开或关闭振动器,第一个值表示在打开振动器之前要等待的毫秒数下一个值表示在关闭振动器之前保持振动器的毫秒数,随后的值交替执行。repeat 振动重复的模式:   -1 为不重复                        0 为一直重复振动                        1 则是指从数组中下标为1的地方开始重复振动(不是振动一次!!!)                               2 从数组中下标为2的地方开始重复振动。                        .....//下面的pattern[]我们可以理解为 开启振动后 等待0.1s振动 振动2s 等待1s 振动1s 等待3slong pattern[] = {100, 2000, 1000, 1000,3000};vibrator.vibrate(pattern,-1); 

对于上面repeat和pattern的关系的还是依照上图来说吧,图片刚好和long pattern[] = {100, 2000, 1000, 1000,3000}数组对应(为了方便解释 我将等待振动时间和振动时间当做一组)当repeat为0 的时候会一直振动 此时会一直走 (0,1),(2,3)下标4刚好是等待时间 依然会执行 然后本次重复结束,开启下一次重复。当repeat 为1 的时候 第一次振动会(0,1),(2,3),然后等待3s,本次振动结束,然后从下标为1的地方开始重复振动, 此时会走(1,2),(3,4),(1,2),(3,4);当repeat为2的时候 第一次振动会(0,1),(2,3),然后等待3s,本次振动结束,从下标为2的地方开始重复振动,此时为(2,3),然后等待3s,结束本次重复,开启下次(2,3)....

**总结:pattern [ ] 数组中第一位为振动等待时间,重复执行时指定的下标为重复振动的第一位,亦为等待时间。
数组中个数为奇数时,最后一位为等待时间,依旧会执行**。

vibrate ( long[ ] pattern, int repeat, AudioAttributes attributes )

这个就不做过多解释,上面都有涉及到。

结束

本篇文章主要介绍了一下振动器常见API和使用,比较简单。但都是亲自测试所得。

更多相关文章

  1. 【Android-tips】 Unable to execute dex: Multiple dex files d
  2. untiy导出android遇到的问题:convert error
  3. Android实现振动效果
  4. android 图片进度条
  5. Android(安卓)Studio问题之dexDebug
  6. 记录代码合并时产生的bug
  7. Android(安卓)DEX方法超过64K和gradle编译OOM解决方案
  8. android 创建快捷方式 删除快捷方式 不重复判断快捷方式
  9. Android震动代码解读

随机推荐

  1. Android架构优秀文章收集
  2. android上传文件
  3. Android各种权限
  4. Android(安卓)源码分析Application的生命
  5. android系统知识
  6. android根据电话号码查询联系人名称,导出
  7. Android用ViewFlipper动态加载图片视图
  8. android http协议post请求方式
  9. Android获取SD卡容量的两种方法
  10. 不能下载Android源码的解决方法