W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
vibrator模塊提供控制馬達振動啟、停的能力。
本模塊首批接口從API version 8開始支持。后續(xù)版本的新增接口,采用上角標單獨標記接口的起始版本。
startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: AsyncCallback<void>): void
根據(jù)指定振動效果和振動屬性觸發(fā)馬達振動。
需要權限:ohos.permission.VIBRATE
系統(tǒng)能力:SystemCapability.Sensors.MiscDevice
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
effect | 是 | 馬達振動效果。 | |
attribute | 是 | 馬達振動屬性。 | |
callback | AsyncCallback<void> | 是 | 回調函數(shù),當馬達振動成功,err為undefined,否則為錯誤對象。 |
錯誤碼:
以下錯誤碼的詳細介紹請參見 ohos.vibrator錯誤碼
錯誤碼ID | 錯誤信息 |
---|---|
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ù)指定振動效果和振動屬性觸發(fā)馬達振動。
需要權限:ohos.permission.VIBRATE
系統(tǒng)能力:SystemCapability.Sensors.MiscDevice
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
effect | 是 | 馬達振動效果。 | |
attribute | 是 | 馬達振動屬性。 |
返回值:
類型 | 說明 |
---|---|
Promise<void> | Promise對象。 |
錯誤碼:
以下錯誤碼的詳細介紹請參見 ohos.vibrator錯誤碼
錯誤碼ID | 錯誤信息 |
---|---|
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
按照指定模式停止馬達的振動。
需要權限:ohos.permission.VIBRATE
系統(tǒng)能力:SystemCapability.Sensors.MiscDevice
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
stopMode | 是 | 指定的停止振動模式。 | |
callback | AsyncCallback<void> | 是 | 回調函數(shù)。當馬達停止振動成功,err為undefined,否則為錯誤對象。 |
示例:
- 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);
- }
- try {
- // 按照VIBRATOR_STOP_MODE_TIME模式停止振動
- 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>
按照指定模式停止馬達的振動。
需要權限:ohos.permission.VIBRATE
系統(tǒng)能力:SystemCapability.Sensors.MiscDevice
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
stopMode | 是 | 馬達停止指定的振動模式。 |
返回值:
類型 | 說明 |
---|---|
Promise<void> | Promise對象。 |
示例:
- 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);
- }
- try {
- // 按照VIBRATOR_STOP_MODE_TIME模式停止振動
- 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);
- }
預置的振動效果。
系統(tǒng)能力:以下各項對應的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice
名稱 | 值 | 說明 |
---|---|---|
EFFECT_CLOCK_TIMER | "haptic.clock.timer" | 描述用戶調整計時器時的振動效果。 |
停止的振動模式。
系統(tǒng)能力:以下各項對應的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice
名稱 | 值 | 說明 |
---|---|---|
VIBRATOR_STOP_MODE_TIME | "time" | 停止模式為duration模式的振動。 |
VIBRATOR_STOP_MODE_PRESET | "preset" | 停止模式為預置EffectId的振動。 |
馬達振動效果。
系統(tǒng)能力:以下各項對應的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice
類型 | 說明 |
---|---|
按照指定持續(xù)時間觸發(fā)馬達振動。 | |
按照預置振動類型觸發(fā)馬達振動。 |
馬達振動時長。
系統(tǒng)能力:以下各項對應的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice
名稱 | 值 | 說明 |
---|---|---|
type | "time" | 按照指定持續(xù)時間觸發(fā)馬達振動。 |
duration | - | 馬達持續(xù)振動時長, 單位ms。 |
馬達預置振動類型。
系統(tǒng)能力:以下各項對應的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice
名稱 | 值 | 說明 |
---|---|---|
type | "preset" | 按照預置振動效果觸發(fā)馬達振動。 |
effectId | - | 預置的振動效果ID。 |
count | - | 重復振動的次數(shù)。 |
馬達振動屬性。
系統(tǒng)能力:以下各項對應的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice
名稱 | 值 | 說明 |
---|---|---|
id | 0 | 振動器id。 |
usage | - | 馬達振動的使用場景。 |
振動使用場景。
系統(tǒng)能力:以下各項對應的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice
名稱 | 類型 | 說明 |
---|---|---|
unknown | string | 沒有明確使用場景,最低優(yōu)先級。 |
alarm | string | 用于警報場景。 |
ring | string | 用于鈴聲場景。 |
notification | string | 用于通知場景。 |
communication | string | 用于通信場景。 |
touch | string | 用于觸摸場景。 |
media | string | 用于多媒體場景。 |
physicalFeedback | string | 用于物理反饋場景。 |
simulateReality | string | 用于模擬現(xiàn)實場景。 |
vibrate(duration: number): Promise<void>
按照指定持續(xù)時間觸發(fā)馬達振動。
從API version 9 開始不再維護,建議使用 vibrator.startVibration 代替。
需要權限:ohos.permission.VIBRATE
系統(tǒng)能力:SystemCapability.Sensors.MiscDevice
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
duration | number | 是 | 馬達振動時長, 單位ms。 |
返回值:
類型 | 說明 |
---|---|
Promise<void> | Promise對象。 |
示例:
- 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ù)時間觸發(fā)馬達振動。
從API version 9 開始不再維護,建議使用 vibrator.startVibration 代替。
需要權限:ohos.permission.VIBRATE
系統(tǒng)能力:SystemCapability.Sensors.MiscDevice
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
duration | number | 是 | 馬達振動時長, 單位ms。 |
callback | AsyncCallback<void> | 否 | 回調函數(shù)。當馬達振動成功,err為undefined,否則為錯誤對象。 |
示例:
- 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>
按照預置振動效果觸發(fā)馬達振動。
從API version 9 開始不再維護,建議使用 vibrator.startVibration 代替。
需要權限:ohos.permission.VIBRATE
系統(tǒng)能力:SystemCapability.Sensors.MiscDevice
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
effectId | 是 | 預置的振動效果ID。 |
返回值:
類型 | 說明 |
---|---|
Promise<void> | Promise對象。 |
示例:
- 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
按照指定振動效果觸發(fā)馬達振動。
從API version 9 開始不再維護,建議使用 vibrator.startVibration 代替。
需要權限:ohos.permission.VIBRATE
系統(tǒng)能力:SystemCapability.Sensors.MiscDevice
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
effectId | 是 | 預置的振動效果ID。 | |
callback | AsyncCallback<void> | 否 | 回調函數(shù)。當馬達振動成功,err為undefined,否則為錯誤對象。 |
示例:
- 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>
按照指定模式停止馬達的振動。
從API version 9 開始不再維護,建議使用 vibrator.stopVibration 代替。
需要權限:ohos.permission.VIBRATE
系統(tǒng)能力:SystemCapability.Sensors.MiscDevice
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
stopMode | 是 | 馬達停止指定的振動模式。 |
返回值:
類型 | 說明 |
---|---|
Promise<void> | Promise對象。 |
示例:
- // 按照effectId類型啟動振動
- 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模式停止振動
- 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
按照指定模式停止馬達的振動。
從API version 9 開始不再維護,建議使用 vibrator.stopVibration 代替。
需要權限:ohos.permission.VIBRATE
系統(tǒng)能力:SystemCapability.Sensors.MiscDevice
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
stopMode | 是 | 馬達停止指定的振動模式。 | |
callback | AsyncCallback<void> | 否 | 回調函數(shù)。當馬達停止振動成功,err為undefined,否則為錯誤對象。 |
示例:
- // 按照effectId類型啟動振動
- 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模式停止振動
- 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號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: