振动
说明: 从API Version 7 开始支持。
导入模块
import vibrate from '@ohos.vibrator';
权限列表
ohos.permission.VIBRATE
vibrate.vibrate
vibrate(duration: number): Promise<void>
按照指定持续时间触发马达振动。
参数:
返回值:
示例:
vibrate.vibrate(100).then(()=>
console.log('Promise returned to indicate a successful vibration.')
).catch((error) => {
console.error('Failed to trigger vibration.');
});
vibrate.vibrate
vibrate(duration: number, callback?: AsyncCallback<void>): void
按照指定持续时间触发马达振动。
参数:
示例:
vibrate.vibrate(100, function(error) {
if (error) {
console.error("Failed to trigger vibration. Error code: " + error.code + ", message: " + error.message);
return;
}
console.info("Succeeded in triggering vibration.");
});
vibrate.vibrate
vibrate(effectId: EffectId): Promise<void>
按照指定振动效果触发马达振动。
参数:
返回值:
示例:
vibrate.vibrate(vibrate.EffectId.EFFECT_CLOCK_TIMER).then(()=>
console.log('Promise returned to indicate a successful vibration.')
).catch((error) => {
console.error('Failed to trigger vibration.');
});
vibrate.vibrate
vibrate(effectId: EffectId, callback?: AsyncCallback<void>): void
按照指定振动效果触发马达振动。
参数:
示例:
vibrate.vibrate(vibrate.EffectId.EFFECT_CLOCK_TIMER, function(error) {
if (error) {
console.error("Failed to trigger vibration. Error code: " + error.code + ", message: " + error.message);
return;
}
console.info("Succeeded in triggering vibration.");
});
vibrate.stop
stop(stopMode: VibratorStopMode): Promise<void>
按照要停止指定的振动模式来停止马达的振动。如果要停止的振动模式与触发马达振动时的模式不相同,则调用本接口会失败。
参数:
返回值:
示例:
vibrate.stop(vibrate.VibratorStopMode.VIBRATOR_STOP_MODE_TIME).then(()=>
console.log('Promise returned to indicate a successful vibration stop.')
).catch((error) => {
console.error('Failed to stop vibration.');
});
vibrate.stop
stop(stopMode: VibratorStopMode, callback?: AsyncCallback<void>): void;
按照要停止指定的振动模式来停止马达的振动。如果要停止的振动模式与触发马达振动时的模式不相同,则调用本接口会失败。
参数:
示例:
vibrate.stop(vibrate.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, function(error) {
if (error) {
console.error("Failed to stop vibration. Error code: " + error.code + ", message: " + error.message);
return;
}
console.info("Vibration stopped.");
});
EffectId
表示马达振动效果的字符串。
VibratorStopMode
表示马达要停止指定的振动模式。
停止模式为预置EffectId的振动。即触发振动时参数类型为EffectId,参数本身为指示马达振动效果的字符串的触发方式。 |