public class VibratorAgent extends Object
This class allows you to query a vibrator based on its ID, check whether the vibrator supports a specific vibration effect, enable the vibrator to vibrate with a certain effect, and disable the vibrator.
VibrationPatternSystemCapability.Sensors#MISC_DEVICE| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static String |
VIBRATOR_STOP_MODE_CUSTOMIZED |
Indicates the mode of stopping a custom vibration effect.
|
static String |
VIBRATOR_STOP_MODE_PRESET |
Indicates the mode of stopping a preset vibration effect.
|
static String |
VIBRATOR_STOP_MODE_TIME |
Indicates the mode of stopping a one-shot vibration effect.
|
| 构造器 | 说明 |
|---|---|
VibratorAgent() |
Default constructor used to create a
VibratorAgent instance. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
List<Integer> |
getVibratorIdList() |
Obtains the IDs of all vibrators on the device.
|
boolean |
isEffectSupport(int vibratorId,
String effectId) |
Checks whether a specified vibrator supports a preset vibration effect.
|
boolean |
isSupport(int vibratorId) |
Checks whether a specified vibrator exists on the hardware device.
|
boolean |
start(int vibratorId,
VibrationPattern vibrationEffect) |
Controls a specified vibrator to vibrate with a custom effect.
|
boolean |
start(String effectId,
boolean isLooping) |
Controls this vibrator to perform a periodic vibration with a preset vibration effect.
|
boolean |
start(VibrationPattern vibrationEffect) |
Controls this vibrator to vibrate with a custom vibration effect.
|
boolean |
startOnce(int duration) |
Controls this vibrator to perform a one-shot vibration at a given duration.
|
boolean |
startOnce(int vibratorId,
int duration) |
Controls a specified vibrator to perform a one-shot vibration at a given duration.
|
boolean |
startOnce(int vibratorId,
String effectId) |
Controls a specified vibrator to perform a one-shot vibration with a preset vibration effect.
|
boolean |
startOnce(String effectId) |
Controls this vibrator to perform a one-shot vibration with a preset vibration effect.
|
boolean |
stop() |
Stops the vibration of this vibrator.
|
boolean |
stop(int vibratorId,
String stopMode) |
Stops the vibration of a vibrator.
|
public static final String VIBRATOR_STOP_MODE_CUSTOMIZED
public static final String VIBRATOR_STOP_MODE_PRESET
public VibratorAgent()
VibratorAgent instance.public List<Integer> getVibratorIdList()
This method can be used when you want to query the IDs of all vibrators on the device.
public boolean isSupport(int vibratorId)
This method can be used when you do not know whether a vibrator exists on the device.
vibratorId - Indicates the ID of the vibrator. You can use the
getVibratorIdList() method to obtain the vibrator ID list.true if the vibrator exists; returns false otherwise.public boolean isEffectSupport(int vibratorId,
String effectId)
Call this method to check whether the vibrator supports a specific vibration effect before enabling it to vibrate with this effect.
vibratorId - Indicates the ID of the vibrator. You can use the
getVibratorIdList() method to obtain the vibrator ID list.effectId - Indicates the preset vibration effect, which is described in
VibrationPattern.true if the vibrator supports the specified vibration effect;
returns false otherwise.public boolean startOnce(int vibratorId,
int duration)
Your application needs to request the ohos.permission.VIBRATE permission
to call this method.
vibratorId - Indicates the ID of the vibrator. You can use the
getVibratorIdList() method to obtain the vibrator ID list.duration - Indicates the duration that the one-shot vibration lasts, in milliseconds.true if the vibrator vibrates as expected; returns false
otherwise, for example, the given duration for the one-shot vibration is invalid.public boolean startOnce(int vibratorId,
String effectId)
Your application needs to request the ohos.permission.VIBRATE permission
to call this method.
vibratorId - Indicates the ID of the vibrator. You can use the
getVibratorIdList() method to obtain the vibrator ID list.effectId - Indicates the preset vibration effect, which is described in
VibrationPattern.true if the vibrator vibrates as expected; returns false
otherwise, for example, the preset vibration effect is not supported.public boolean start(int vibratorId,
VibrationPattern vibrationEffect)
You can customize a vibration effect in
VibrationPattern. Your application needs to request the
ohos.permission.VIBRATE permission to call this method.
vibratorId - Indicates the ID of the vibrator. You can use the
getVibratorIdList() method to obtain the vibrator ID list.vibrationEffect - Indicates a custom vibration effect.
You can customize the effect of a one-shot vibration using the
VibrationPattern.createSingle(int, int) method, and
customize the effect of a periodic vibration using the
VibrationPattern.createPeriod(int[], int) or
VibrationPattern.createPeriod(int[], int[], int) method.true if the vibrator vibrates as expected; returns false
otherwise, for example, the custom vibration effect is not supported.VibrationPatternpublic boolean startOnce(int duration)
Your application needs to request the ohos.permission.VIBRATE permission
to call this method.
duration - Indicates the duration that the one-shot vibration lasts, in milliseconds.true if the vibrator vibrates as expected; returns false
otherwise, for example, the given duration for the one-shot vibration is invalid.public boolean startOnce(String effectId)
Your application needs to request the ohos.permission.VIBRATE permission
to call this method.
effectId - Indicates the preset vibration effect, which is described in
VibrationPattern.true if the vibrator vibrates as expected; returns false
otherwise, for example, the preset vibration effect is not supported.public boolean start(String effectId, boolean isLooping)
effectId - Indicates the preset vibration effect, which is described in VibrationPattern.isLooping - Specifies whether to start a periodic vibration. The value true
means to start a periodic vibration, and false means the opposite.true if the vibrator vibrates as expected; returns false
otherwise, for example, the preset vibration effect does not support periodic vibration.public boolean start(VibrationPattern vibrationEffect)
You can customize a vibration effect in
VibrationPattern. Your application needs to request the
ohos.permission.VIBRATE permission to call this method.
vibrationEffect - Indicates a custom vibration effect.
You can customize the effect of a one-shot vibration using the
VibrationPattern.createSingle(int, int) method, and
customize the effect of a periodic vibration using the
VibrationPattern.createPeriod(int[], int) or
VibrationPattern.createPeriod(int[], int[], int) method.true if the vibrator vibrates as expected; returns false
otherwise, for example, the custom vibration effect is not supported.VibrationPatternpublic boolean stop(int vibratorId,
String stopMode)
This method can be used when you no longer need the vibrator to vibrate.
vibratorId - Indicates the ID of the vibrator. You can use the
getVibratorIdList() method to obtain the vibrator ID list.stopMode - Indicates the mode of the vibration to stop. The values can be time,
preset, and customized, respectively representing a one-shot vibration
effect, a preset vibration effect, and a custom vibration effect.true if the vibration is normally stopped; returns false
otherwise, for example, the specified stop mode is invalid.public boolean stop()
This method can be used when you no longer need the vibrator to vibrate.
true if the vibration is normally stopped; returns false
otherwise.