codecamp

鸿蒙OS Vibrator开发指导

场景介绍

当设备需要设置不同的振动效果时,可以调用 Vibrator 模块,例如,设备的按键可以设置不同强度和时长的振动,闹钟和来电可以设置不同强度和时长的单次或周期性振动。

接口说明

振动器模块主要提供的功能有:查询设备上振动器的列表,查询某个振动器是否支持某种振动效果,触发和关闭振动器。VibratorAgent 类开放能力如下,具体请查阅 API 参考文档。

接口名 描述
getVibratorIdList() 获取硬件设备上的振动器列表。
isSupport(int) 根据指定的振动器Id查询硬件设备是否存在该振动器。
isEffectSupport(int, String) 查询指定的振动器是否支持指定的震动效果。
startOnce(int, String) 对指定的振动器创建指定效果的一次性振动。
startOnce(String) 对指定的振动器创建指定效果的一次性振动。
startOnce(int, int) 对指定的振动器创建指定振动时长的一次性振动。
startOnce(int) 对指定的振动器创建指定振动时长的一次性振动。
start(int, VibrationPattern) 对指定的振动器创建自定义效果的波形或一次性振动。
start(VibrationPattern) 对指定的振动器创建自定义效果的波形或一次性振动。
stop(int, String) 关闭指定的振动器指定模式的振动。
stop(String) 关闭指定的振动器指定模式的振动。

开发步骤

  1. 控制设备上的振动器,需要在“config.json”里面进行配置请求权限。具体如下:

   "reqPermissions": [
       {
           "name": "ohos.permission.VIBRATE",
           "reason": "",
           "usedScene": {
               "ability": [
                   ".MainAbility"
               ],
               "when": "inuse"
           }
       }
   ]

  1. 查询硬件设备上的振动器列表。

  1. 查询指定的振动器是否支持指定的震动效果。

  1. 创建不同效果的振动。

  1. 关闭指定的振动器指定模式的振动。

   private VibratorAgent vibratorAgent = new VibratorAgent();

    
   private int[] timing = {1000, 1000, 2000, 5000};

    
   private int[] intensity = {50, 100, 200, 255};

    
   @Override
   public void onStart(Intent intent) {
       super.onStart(intent);
       super.setUIContent(ResourceTable.Layout_vibrator_layout);

    
       // ...

    
       // 查询硬件设备上的振动器列表
       List<Integer> vibratorList = vibratorAgent.getVibratorIdList();
       if (vibratorList.isEmpty()) {
           return;
       }
       int vibratorId = vibratorList.get(0);

    
       // 查询指定的振动器是否支持指定的振动效果
       boolean isSupport = vibratorAgent.isEffectSupport(vibratorId,
               VibrationPattern.VIBRATOR_TPYE_CAMERA_CLICK);

    
       // 创建指定效果的一次性振动
       boolean vibrateEffectResult = vibratorAgent.vibrate(vibratorId,
               VibrationPattern.VIBRATOR_TPYE_CAMERA_CLICK);

    
       // 创建指定振动时长的一次性振动
       int vibratorTiming = 1000;
       boolean vibrateResult = vibratorAgent.vibrate(vibratorId, vibratorTiming);

    
       // 创建自定义效果的周期性波形振动
       int count = 5;
       VibrationPattern vibrationPeriodEffect = VibrationPattern.createPeriod(timing, intensity, count);
       boolean vibratePeriodResult = vibratorAgent.vibrate(vibratorId, vibrationPeriodEffect);

    
       // 创建自定义效果的一次性振动
       VibrationPattern vibrationOnceEffect = VibrationPattern.createSingle(3000, 50);
       boolean vibrateSingleResult = vibratorAgent.vibrate(vibratorId, vibrationOnceEffect);

    
       // 关闭指定的振动器自定义模式的振动
       boolean stopResult = vibratorAgent.stop(vibratorId,
               VibratorAgent.VIBRATOR_STOP_MODE_CUSTOMIZED);
   }
鸿蒙OS Light开发指导
鸿蒙OS 位置概述
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

鸿蒙OS 开发

鸿蒙OS 术语

鸿蒙OS Java API参考

鸿蒙OS ohos.aafwk.ability

鸿蒙OS ohos.aafwk.abilityjet.activedata

鸿蒙OS ohos.aafwk.content

鸿蒙OS java.lang

鸿蒙OS java.Util

鸿蒙OS java.Util class

鸿蒙OS ohos.data.dataability

鸿蒙OS ohos.data.dataability class

鸿蒙OS ohos.agp.components

鸿蒙OS ohos.agp.components interface

鸿蒙OS ohos.agp.components class

鸿蒙OS ohos.global.configuration

鸿蒙OS java.io

鸿蒙OS ohos.data.resultset

鸿蒙OS ohos.data.resultset interface

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }