转载文章请注明出处:http://blog.csdn.net/dangxw_/article/details/17957175

android 控制手机震动的类为Vibrator 他所提供的方法只有四个


想要控制手机的震动强度,无疑就只能使用vibrate(long[] parttern,int repeat)方法了,查看这个方法的详解:

Vibrate with a given 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.

To cause the pattern to repeat, pass the index into the pattern array at which to start the repeat, or -1 to disable repeating.

This method requires the caller to hold the permission VIBRATE.

Parameters
pattern an array of longs of times for which to turn the vibrator on or off.
repeat the index into pattern at which to repeat, or -1 if you don't want to repeat.

也就是说根据所提供的数值参数进行震动,也就是数组pattern ,不过先谈一下repeat,这是个震动模式阐述,经过我的测试,-1为不循环震动,1为最高模式循环震动,2为所给参数的格式循环震动。

pattern数组可以调整手机的震动强度,因为是以毫秒计数的,时间特别的短,不停地开和关让人感觉不到间断,而能感觉到震动强弱,就类似于电子器件的调整占空比达到理想的电压。

比如:{100,100,100,100}就是指的震动电机先关闭0.1秒再震动0.1秒,如此循环两次,如果将repeat参数定义为2,则会不停地以这种方式震动,与最高级的震动幅度相比,就相当于震动幅度缩小了一倍。

另外为了使震动幅度可以线性变化,可以定义long数组为{100-i,100+i,100-i,100+i}同时将repeat参数定义为2,调整i的大小,不停地调用vibrate方法就可以动态改变手机的震动强度。

vibrator类是系统提供的服务,只能通过getSystemService(Context.VIBRATOR_SERVICE)方法获取,另外还要给予调用权限:


比较可悲的是ios系统只提供震动功能的调用,不可以定义震动幅度和时长,只有一种模式。不过它的系统提供一种供用户自己手动绘制震动模式的插件。

更多相关文章

  1. Android使用广播(BroadCast)实现强制下线的方法
  2. Google Map API Key 获得方法
  3. android 植入点乐广告方法
  4. UI与线程交互
  5. RecyclerView之ItemDecoration由浅入深
  6. Android(安卓)使用Post与Get方法进行表单提交数据
  7. iOS url 编码 解码
  8. [Android] S​Q​l​i​t​e​数​据​库
  9. Android学习系列(43)--使用事件总线框架EventBus和Otto

随机推荐

  1. Android兼容性测试工具Spoon
  2. android——再谈加载大量图片性能问题
  3. Android清单文件详解(一) ---- 一切从开
  4. 利用任务调度特性检测Android模拟器
  5. Android VR Player(全景视频播放器) [6]:视
  6. Android实现屏幕旋转方法总结
  7. Android中线程形态AsyncTask、HandlerThr
  8. Android和IPhone手机端开发私活
  9. TabActivity实现多页显示效果
  10. android XML解析方式