harmony 鸿蒙Vibrator Development
Vibrator Development
When to Use
You can set different vibration effects for your device, for example, you can set vibration effects with different intensities and durations for buttons on the device.
For details about the APIs, see Vibrator.
Available APIs
Name | Description |
---|---|
startVibration(effect: VibrateTime, attribute: VibrateAttribute): Promise<void> | Starts vibration with the specified effect and attribute. This API uses a promise to return the result. |
startVibration(effect: VibrateTime, attribute: VibrateAttribute, callback: AsyncCallback<void>): void | Starts vibration with the specified effect and attribute. This API uses an asynchronous callback to return the result. |
stopVibration(): Promise<void> | Stops vibration in all modes. This API uses a promise to return the result. |
stopVibration(callback: AsyncCallback<void>): void | Stops vibration in all modes. This API uses an asynchronous callback to return the result. |
How to Develop
Before using the vibrator on a device, you must declare the ohos.permission.VIBRATE permission. For details, see Declaring Permissions.
Configure the vibrator to vibrate with the specified duration and attribute.
import { vibrator } from '@kit.SensorServiceKit'; import { BusinessError } from '@kit.BasicServicesKit'; try { // Start vibration. vibrator.startVibration({ type: 'time', duration: 1000, }, { id: 0, usage: 'alarm' }, (error: BusinessError) => { if (error) { console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); return; } console.info('Succeed in starting vibration'); }); } catch (err) { let e: BusinessError = err as BusinessError; console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); }
Stop vibration.
import { vibrator } from '@kit.SensorServiceKit'; import { BusinessError } from '@kit.BasicServicesKit'; try { // Stop vibration in all modes. vibrator.stopVibration((error: BusinessError) => { if (error) { console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); return; } console.info('Succeed in stopping vibration'); }) } catch (error) { let e: BusinessError = error as BusinessError; console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); }
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Sensor Development (C/C++)
harmony 鸿蒙Sensor Development (ArkTS)
harmony 鸿蒙Introduction to Sensor Service Kit
harmony 鸿蒙Vibrator Development (C/C++)
0
赞
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦