image

前言

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」>「等待3s」long 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(安卓)防止Toast重复弹出相同的信息
  2. 【JAVA】生成一个32位的随机数。防止重复,保留唯一性
  3. android fragment 重复创建的问题
  4. ionic 打包报某些jar包重复
  5. Android(安卓)防止多次重复点击的三种方法
  6. ListView为什么要使用convertView和ViewHolder
  7. Android在ListView显示图片(重复错乱闪烁问题)
  8. android中Toast重复显示bug解决方法
  9. Android中的Toast重复显示的问题

随机推荐

  1. android设备添加F1-F12按键功能
  2. android之Tabhost深入讲解二
  3. Android动画播放的常用方式
  4. ContentProvider 翻译
  5. android获得屏幕高度和宽度
  6. Android应用程序安装过程源代码分析(3)
  7. android 获取界面上所有控件
  8. Android开发——控件基础(七)ListView组件:
  9. Android中使用Gallery_tabhost来实现标签
  10. alps/frameworks/base/wifi/java/android