W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
vibrator模塊提供控制馬達(dá)振動(dòng)啟、停的能力。
本模塊首批接口從API version 8開(kāi)始支持。后續(xù)版本的新增接口,采用上角標(biāo)單獨(dú)標(biāo)記接口的起始版本。
startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: AsyncCallback<void>): void
根據(jù)指定振動(dòng)效果和振動(dòng)屬性觸發(fā)馬達(dá)振動(dòng)。
需要權(quán)限:ohos.permission.VIBRATE
系統(tǒng)能力:SystemCapability.Sensors.MiscDevice
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
effect | 是 | 馬達(dá)振動(dòng)效果。 | |
attribute | 是 | 馬達(dá)振動(dòng)屬性。 | |
callback | AsyncCallback<void> | 是 | 回調(diào)函數(shù),當(dāng)馬達(dá)振動(dòng)成功,err為undefined,否則為錯(cuò)誤對(duì)象。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn) ohos.vibrator錯(cuò)誤碼
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
14600101 | Device operation failed. |
示例:
- try {
- vibrator.startVibration({
- type: 'time',
- duration: 1000,
- }, {
- id: 0,
- usage: 'alarm'
- }, (error) => {
- if (error) {
- console.error('vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message);
- return;
- }
- console.log('Callback returned to indicate a successful vibration.');
- });
- } catch (err) {
- console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
- }
startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise<void>
根據(jù)指定振動(dòng)效果和振動(dòng)屬性觸發(fā)馬達(dá)振動(dòng)。
需要權(quán)限:ohos.permission.VIBRATE
系統(tǒng)能力:SystemCapability.Sensors.MiscDevice
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
effect | 是 | 馬達(dá)振動(dòng)效果。 | |
attribute | 是 | 馬達(dá)振動(dòng)屬性。 |
返回值:
類型 | 說(shuō)明 |
---|---|
Promise<void> | Promise對(duì)象。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn) ohos.vibrator錯(cuò)誤碼
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
14600101 | Device operation failed. |
示例:
- try {
- vibrator.startVibration({
- type: 'time',
- duration: 1000
- }, {
- id: 0,
- usage: 'alarm'
- }).then(() => {
- console.log('Promise returned to indicate a successful vibration');
- }, (error) => {
- console.error('error.code' + error.code + 'error.message' + error.message);
- });
- } catch (err) {
- console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
- }
stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback<void>): void
按照指定模式停止馬達(dá)的振動(dòng)。
需要權(quán)限:ohos.permission.VIBRATE
系統(tǒng)能力:SystemCapability.Sensors.MiscDevice
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
stopMode | 是 | 指定的停止振動(dòng)模式。 | |
callback | AsyncCallback<void> | 是 | 回調(diào)函數(shù)。當(dāng)馬達(dá)停止振動(dòng)成功,err為undefined,否則為錯(cuò)誤對(duì)象。 |
示例:
- try {
- // 按照固定時(shí)長(zhǎng)振動(dòng)
- vibrator.startVibration({
- type: 'time',
- duration: 1000,
- }, {
- id: 0,
- usage: 'alarm'
- }, (error) => {
- if (error) {
- console.error('vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message);
- return;
- }
- console.log('Callback returned to indicate a successful vibration.');
- });
- } catch (err) {
- console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
- }
- try {
- // 按照VIBRATOR_STOP_MODE_TIME模式停止振動(dòng)
- vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, function (error) {
- if (error) {
- console.log('error.code' + error.code + 'error.message' + error.message);
- return;
- }
- console.log('Callback returned to indicate successful.');
- })
- } catch (err) {
- console.info('errCode: ' + err.code + ' ,msg: ' + err.message);
- }
stopVibration(stopMode: VibratorStopMode): Promise<void>
按照指定模式停止馬達(dá)的振動(dòng)。
需要權(quán)限:ohos.permission.VIBRATE
系統(tǒng)能力:SystemCapability.Sensors.MiscDevice
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
stopMode | 是 | 馬達(dá)停止指定的振動(dòng)模式。 |
返回值:
類型 | 說(shuō)明 |
---|---|
Promise<void> | Promise對(duì)象。 |
示例:
- try {
- // 按照固定時(shí)長(zhǎng)振動(dòng)
- vibrator.startVibration({
- type: 'time',
- duration: 1000
- }, {
- id: 0,
- usage: 'alarm'
- }).then(() => {
- console.log('Promise returned to indicate a successful vibration');
- }, (error) => {
- console.error('error.code' + error.code + 'error.message' + error.message);
- });
- } catch (err) {
- console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
- }
- try {
- // 按照VIBRATOR_STOP_MODE_TIME模式停止振動(dòng)
- vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME).then(() => {
- console.log('Promise returned to indicate a successful vibration.');
- }, (error) => {
- console.log('error.code' + error.code + 'error.message' + error.message);
- });
- } catch (err) {
- console.info('errCode: ' + err.code + ' ,msg: ' + err.message);
- }
預(yù)置的振動(dòng)效果。
系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice
名稱 | 值 | 說(shuō)明 |
---|---|---|
EFFECT_CLOCK_TIMER | "haptic.clock.timer" | 描述用戶調(diào)整計(jì)時(shí)器時(shí)的振動(dòng)效果。 |
停止的振動(dòng)模式。
系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice
名稱 | 值 | 說(shuō)明 |
---|---|---|
VIBRATOR_STOP_MODE_TIME | "time" | 停止模式為duration模式的振動(dòng)。 |
VIBRATOR_STOP_MODE_PRESET | "preset" | 停止模式為預(yù)置EffectId的振動(dòng)。 |
馬達(dá)振動(dòng)效果。
系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice
類型 | 說(shuō)明 |
---|---|
按照指定持續(xù)時(shí)間觸發(fā)馬達(dá)振動(dòng)。 | |
按照預(yù)置振動(dòng)類型觸發(fā)馬達(dá)振動(dòng)。 |
馬達(dá)振動(dòng)時(shí)長(zhǎng)。
系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice
名稱 | 值 | 說(shuō)明 |
---|---|---|
type | "time" | 按照指定持續(xù)時(shí)間觸發(fā)馬達(dá)振動(dòng)。 |
duration | - | 馬達(dá)持續(xù)振動(dòng)時(shí)長(zhǎng), 單位ms。 |
馬達(dá)預(yù)置振動(dòng)類型。
系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice
名稱 | 值 | 說(shuō)明 |
---|---|---|
type | "preset" | 按照預(yù)置振動(dòng)效果觸發(fā)馬達(dá)振動(dòng)。 |
effectId | - | 預(yù)置的振動(dòng)效果ID。 |
count | - | 重復(fù)振動(dòng)的次數(shù)。 |
馬達(dá)振動(dòng)屬性。
系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice
名稱 | 值 | 說(shuō)明 |
---|---|---|
id | 0 | 振動(dòng)器id。 |
usage | - | 馬達(dá)振動(dòng)的使用場(chǎng)景。 |
振動(dòng)使用場(chǎng)景。
系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice
名稱 | 類型 | 說(shuō)明 |
---|---|---|
unknown | string | 沒(méi)有明確使用場(chǎng)景,最低優(yōu)先級(jí)。 |
alarm | string | 用于警報(bào)場(chǎng)景。 |
ring | string | 用于鈴聲場(chǎng)景。 |
notification | string | 用于通知場(chǎng)景。 |
communication | string | 用于通信場(chǎng)景。 |
touch | string | 用于觸摸場(chǎng)景。 |
media | string | 用于多媒體場(chǎng)景。 |
physicalFeedback | string | 用于物理反饋場(chǎng)景。 |
simulateReality | string | 用于模擬現(xiàn)實(shí)場(chǎng)景。 |
vibrate(duration: number): Promise<void>
按照指定持續(xù)時(shí)間觸發(fā)馬達(dá)振動(dòng)。
從API version 9 開(kāi)始不再維護(hù),建議使用 vibrator.startVibration 代替。
需要權(quán)限:ohos.permission.VIBRATE
系統(tǒng)能力:SystemCapability.Sensors.MiscDevice
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
duration | number | 是 | 馬達(dá)振動(dòng)時(shí)長(zhǎng), 單位ms。 |
返回值:
類型 | 說(shuō)明 |
---|---|
Promise<void> | Promise對(duì)象。 |
示例:
- vibrator.vibrate(1000).then(() => {
- console.log('Promise returned to indicate a successful vibration.');
- }, (error) => {
- console.log('error.code' + error.code + 'error.message' + error.message);
- });
vibrate(duration: number, callback?: AsyncCallback<void>): void
按照指定持續(xù)時(shí)間觸發(fā)馬達(dá)振動(dòng)。
從API version 9 開(kāi)始不再維護(hù),建議使用 vibrator.startVibration 代替。
需要權(quán)限:ohos.permission.VIBRATE
系統(tǒng)能力:SystemCapability.Sensors.MiscDevice
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
duration | number | 是 | 馬達(dá)振動(dòng)時(shí)長(zhǎng), 單位ms。 |
callback | AsyncCallback<void> | 否 | 回調(diào)函數(shù)。當(dāng)馬達(dá)振動(dòng)成功,err為undefined,否則為錯(cuò)誤對(duì)象。 |
示例:
- vibrator.vibrate(1000, function (error) {
- if (error) {
- console.log('error.code' + error.code + 'error.message' + error.message);
- } else {
- console.log('Callback returned to indicate a successful vibration.');
- }
- })
vibrate(effectId: EffectId): Promise<void>
按照預(yù)置振動(dòng)效果觸發(fā)馬達(dá)振動(dòng)。
從API version 9 開(kāi)始不再維護(hù),建議使用 vibrator.startVibration 代替。
需要權(quán)限:ohos.permission.VIBRATE
系統(tǒng)能力:SystemCapability.Sensors.MiscDevice
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
effectId | 是 | 預(yù)置的振動(dòng)效果ID。 |
返回值:
類型 | 說(shuō)明 |
---|---|
Promise<void> | Promise對(duì)象。 |
示例:
- vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(() => {
- console.log('Promise returned to indicate a successful vibration.');
- }, (error) => {
- console.log('error.code' + error.code + 'error.message' + error.message);
- });
vibrate(effectId: EffectId, callback?: AsyncCallback<void>): void
按照指定振動(dòng)效果觸發(fā)馬達(dá)振動(dòng)。
從API version 9 開(kāi)始不再維護(hù),建議使用 vibrator.startVibration 代替。
需要權(quán)限:ohos.permission.VIBRATE
系統(tǒng)能力:SystemCapability.Sensors.MiscDevice
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
effectId | 是 | 預(yù)置的振動(dòng)效果ID。 | |
callback | AsyncCallback<void> | 否 | 回調(diào)函數(shù)。當(dāng)馬達(dá)振動(dòng)成功,err為undefined,否則為錯(cuò)誤對(duì)象。 |
示例:
- vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function (error) {
- if (error) {
- console.log('error.code' + error.code + 'error.message' + error.message);
- } else {
- console.log('Callback returned to indicate a successful vibration.');
- }
- })
stop(stopMode: VibratorStopMode): Promise<void>
按照指定模式停止馬達(dá)的振動(dòng)。
從API version 9 開(kāi)始不再維護(hù),建議使用 vibrator.stopVibration 代替。
需要權(quán)限:ohos.permission.VIBRATE
系統(tǒng)能力:SystemCapability.Sensors.MiscDevice
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
stopMode | 是 | 馬達(dá)停止指定的振動(dòng)模式。 |
返回值:
類型 | 說(shuō)明 |
---|---|
Promise<void> | Promise對(duì)象。 |
示例:
- // 按照effectId類型啟動(dòng)振動(dòng)
- vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function (error) {
- if (error) {
- console.log('error.code' + error.code + 'error.message' + error.message);
- } else {
- console.log('Callback returned to indicate a successful vibration.');
- }
- })
- // 使用VIBRATOR_STOP_MODE_PRESET模式停止振動(dòng)
- vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => {
- console.log('Promise returned to indicate a successful vibration.');
- }, (error) => {
- console.log('error.code' + error.code + 'error.message' + error.message);
- });
stop(stopMode: VibratorStopMode, callback?: AsyncCallback<void>): void
按照指定模式停止馬達(dá)的振動(dòng)。
從API version 9 開(kāi)始不再維護(hù),建議使用 vibrator.stopVibration 代替。
需要權(quán)限:ohos.permission.VIBRATE
系統(tǒng)能力:SystemCapability.Sensors.MiscDevice
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
stopMode | 是 | 馬達(dá)停止指定的振動(dòng)模式。 | |
callback | AsyncCallback<void> | 否 | 回調(diào)函數(shù)。當(dāng)馬達(dá)停止振動(dòng)成功,err為undefined,否則為錯(cuò)誤對(duì)象。 |
示例:
- // 按照effectId類型啟動(dòng)振動(dòng)
- vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function (error) {
- if (error) {
- console.log('error.code' + error.code + 'error.message' + error.message);
- } else {
- console.log('Callback returned to indicate a successful vibration.');
- }
- })
- // 使用VIBRATOR_STOP_MODE_PRESET模式停止振動(dòng)
- vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, function (error) {
- if (error) {
- console.log('error.code' + error.code + 'error.message' + error.message);
- } else {
- console.log('Callback returned to indicate successful.');
- }
- })
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: