【关键字】
震动 / 调用 / 无效 / CPU
【问题描述】
调用手机震动接口无效果,示例代码如下。
doVibrate(){
try {
vibrator.startVibration({
type: 'preset',
effectId: 'haptic.clock.timer',
count: 1,
}, {
id: 0,
usage: 'alarm'
}, (error: BusinessError) => {
if (error) {
GmuLogProxy.error(tag,`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
return;
}
GmuLogProxy.info(tag,'Succeed in starting vibration');
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
}
}
在API 11真机上进行测试,发现手机并没有震动。
【解决方案】
使用振动器,需要申请ohos.permission.VIBRATE权限,在module.json5配置文件的requestPermissions标签里加上此权限。
具体参考文档: