背景

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.  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.* @param attributes {@link AudioAttributes} corresponding to the vibration. For example,*                   specify {@link AudioAttributes#USAGE_ALARM} for alarm vibrations or*                   {@link AudioAttributes#USAGE_NOTIFICATION_RINGTONE} for*                   vibrations associated with incoming calls.* @deprecated Use {@link #vibrate(VibrationEffect, AudioAttributes)} instead.*/@Deprecated@RequiresPermission(android.Manifest.permission.VIBRATE)public void vibrate(long[] pattern, int repeat, AudioAttributes attributes) {// This call needs to continue throwing ArrayIndexOutOfBoundsException but ignore all other// exceptions for compatibility purposesif (repeat < -1 || repeat >= pattern.length) {Log.e(TAG, "vibrate called with repeat index out of bounds" +(pattern.length=" + pattern.length + ", index=" + repeat +")");throw new ArrayIndexOutOfBoundsException();}try {vibrate(VibrationEffect.createWaveform(pattern, repeat), attributes);} catch (IllegalArgumentException iae) {Log.e(TAG, "Failed to create VibrationEffect", iae);}}

所以我们需要在清单文件中添加vibrator使用的权限

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

2,实现调用,vibrator实现灭屏震动依赖AudioAttributes的设置,这里我们需要使用USAGE_ALARM作为类型

AudioAttributes VIBRATION_ATTRIBUTES = new AudioAttributes.Builder().setContentType(AudioAttributes.USAGE_ALARM) /*USAGE_ALARM*/.setUsage(AudioAttributes.USAGE_ALARM).build();

获得震动服务的句柄

Vibrator mVibrator;
mVibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);

调用系统函数,函数的参数说明文章开头有介绍

mVibrator.vibrate(new long[]{1000, 2000, 3000, 4000}, 0, VIBRATION_ATTRIBUTES);

实现的关键是AudioAttributes的属性设置,不同的设置实现的效果不一样,灭屏震动,可以监听屏幕的状态,在灭屏时调用上述方法,使马达运行。

3,取消震动

mVibrator.cancel()

Android Vibrator 实现灭屏震动功能相关推荐

  1. android流程点击开机键熄屏,一种基于android系统的灭屏状态下指纹解锁加速亮屏方法与流程...

    本发明涉及android系统解锁显示方法,尤其涉及一种基于android系统的灭屏状态下指纹解锁加速亮屏方法. 背景技术: 目前,随着指纹技术越来越普及,很多android系统设备都带有指纹外设,特别 ...

  2. 结合源码探讨Android距离传感器亮灭屏机制

    结合源码探讨Android距离传感器亮灭屏机制 本文的分析是基于Android 5.0.0 Google原生代码的. Android中的距离传感器,也就是P-sensors(Proximity Sen ...

  3. Android如何实现灭屏功能

    在Android系统中,可以通过使用PowerManager类来实现灭屏功能.你可以在代码中调用PowerManager.goToSleep()方法来实现灭屏.需要注意的是,在使用这个方法前,你需要获 ...

  4. android 华为手机灭屏搜索不到蓝牙_华为Mate 30更新EMUI10.1.0.132版本,新增10项实用功能...

    前几天,华为新版本内测开始,无线传声功能备受欢迎.今天,小向的华为mate 30也终于迎来了更新,除了无线传声,还有10多项新功能值得期待,不知道你有没有更新呢? 宣传卖点:无线传声 无线传声,是华为 ...

  5. Android Keyguard 亮灭屏流程分析

    文章目录 概述 Keyguard 关键文件 Power 灭屏 超时灭屏 Power 亮屏 FAQ 亮屏慢 概述 Keyguard 锁屏流程: Keyguard 加载时机:一个是开机的时候:另一个是灭屏 ...

  6. Android 给App加上屏保功能 类似广告功能的实现。

    一朋友的所做的app中需要每个activity在用户30秒没有任何操作的时候,弹出一个屏保,屏保保持是屏幕长亮,屏保上面可以实现广告啊什么的等等.于是就写了个小demo,基本差不多都实现了,只需要改改 ...

  7. android源码灭屏时蓝牙自动配对

    1.android4.4/packages/apps/Settings/src/com/android/settings/bluetooth/BluetoothPairingDialog.java 添 ...

  8. Android P Keyguard Scrim快速灭屏亮屏闪亮

    MTK在android P上会出现在设置锁屏滑动时候,会出现快速灭屏亮屏闪亮现象. 在开发者模式关掉动画时不会出现此问题(Keyguard Scrim动画问题) 解决办法:setDuration(0) ...

  9. Android 灭屏和亮屏的监听

    采用动态注册广播的方式对灭屏和亮屏进行监听 1.创建receiver class ScreenStatusReceiver extends BroadcastReceiver { ​String SC ...

最新文章

  1. python 执行vba脚本_用python批量执行VBA代码
  2. linux 误删除mysql表能恢复吗_Linux下Oracle误删除数据文件恢复操作
  3. Vue iView Admin 动态路由菜单加载 前后端分离(springboot 2.x iview admin vue 前后端分离 模型设计器 动态数据权限...
  4. 【Scratch】青少年蓝桥杯_每日一题_2.17_城堡
  5. oid 值 内存使用_如何使用Choerodon LDAP以及配置定时任务
  6. 轮换html有虚宽出现,乒乓球理论考试复习资料
  7. java 析构函数_C++虚函数
  8. 大疆云台如何使用华为mate20pro_华为Mate30+大疆灵眸Osmo3,让你的照片和短视频称霸朋友圈...
  9. Spark 实践——用 Scala 和 Spark 进行数据分析
  10. java参考文献英文_java论文英文的参考文献
  11. 【笔记】概统论与数理统计第四章知识点总结
  12. 通信系统中语音信号的仿真分析
  13. oracle数据库怎么切换实例,oracle切换数据库实例
  14. 程序员的自我修养(雾)
  15. python人脸融合_使用 python 进行 面部合成
  16. 【高危安全通告】微软8月多个漏洞修复
  17. STM32实现德飞莱LED滚动效果
  18. 乔恩·斯凯特(Jon Skeet)-编程的查克·诺里斯(Chuck Norris)
  19. 《AI Utopia or Dystopia (DAC‘20)》阅读笔记
  20. 细胞重编程技术方法学评估

热门文章

  1. java小项目鲜花销售系统
  2. filter() 函数的用法
  3. Matlab Serial Port学习
  4. 简比:三大电商巨头的平台、支付、物流
  5. 训练赛Day6-The 36th ACMICPC Asia Regional Beijing Site
  6. element ui rules校验记录,方便后续使用
  7. 明佳妈妈新营销谈专业分工战力倍增
  8. createNewFile创建文件失败 系统找不到指定的路径。。。。。
  9. 程序员的假期总结——竹子(一)
  10. javascript条件语句if else用法