背景
vibrator 是手机上的一个硬件功能,也是常用的提示功能,在开发过程中,遇到一个需求,需要实现灭屏震动提示,搜集网上博客,没有找到想要的案例,所以自己花了点时间研究了,记录下实现
android 对硬件的权限使用控制的越来越严格,android R 之前的灭屏震动不知道有没有特别设置,本文只讨论Android R上的应用。
正文
1,在AndroidManifest.xml中添加使用到的控制权限,为什么需要?阅读源码可以看到调用vibrate方法的时候会对权限检查,如果没有申请,那么将会导致应用crash
/**
* 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. Subsequen