Vibrator  |  API reference  |  Android Developers


public abstract class Vibrator
extends Object



Class that operates the vibrator on the device.

If your process exits, any vibration you started will stop.

Summary

Constants

int VIBRATION_EFFECT_SUPPORT_NO

Vibration effect support: unsupported

This effect is not natively supported by the underlying hardware, although the system may still play a fallback vibration.

int VIBRATION_EFFECT_SUPPORT_UNKNOWN

Vibration effect support: unknown

The hardware doesn't report its supported effects, so we can't determine whether the effect is supported or not.

int VIBRATION_EFFECT_SUPPORT_YES

Vibration effect support: supported

This effect is supported by the underlying hardware.

Public methods

final int areAllEffectsSupported(int... effectIds)

Query whether the vibrator supports all the given effects.

final boolean areAllPrimitivesSupported(int... primitiveIds)

Query whether the vibrator supports all of the given primitives.

int[] areEffectsSupported(int... effectIds)

Query whether the vibrator natively supports the given effects.

boolean areEnvelopeEffectsSupported()

Checks whether the vibrator supports the creation of envelope effects.

boolean[] arePrimitivesSupported(int... primitiveIds)

Query whether the vibrator supports the given primitives.

abstract void cancel()

Turn the vibrator off.

VibratorEnvelopeEffectInfo getEnvelopeEffectInfo()

Retrieves the vibrator's capabilities and limitations for envelope effects.

VibratorFrequencyProfile getFrequencyProfile()

Gets the profile that describes the vibrator output across the supported frequency range.

int getId()

Return the ID of this vibrator.

int[] getPrimitiveDurations(int... primitiveIds)

Query the estimated durations of the given primitives.

float getQFactor()

Gets the Q factor of the vibrator.

float getResonantFrequency()

Gets the resonant frequency of the vibrator, if applicable.

abstract boolean hasAmplitudeControl()

Check whether the vibrator has amplitude control.

abstract boolean hasVibrator()

Check whether the hardware has a vibrator.

void vibrate(long milliseconds)

This method was deprecated in API level 26. Use vibrate(VibrationEffect) instead.

void vibrate(VibrationEffect vibe, VibrationAttributes attributes)

Vibrate with a given effect.

void vibrate(long[] pattern, int repeat)

This method was deprecated in API level 26. Use vibrate(VibrationEffect) instead.

void vibrate(VibrationEffect vibe, AudioAttributes attributes)

This method was deprecated in API level 33. Use vibrate(VibrationEffect,VibrationAttributes) instead.

void vibrate(long milliseconds, AudioAttributes attributes)

This method was deprecated in API level 26. Use vibrate(VibrationEffect,VibrationAttributes) instead.

void vibrate(VibrationEffect vibe)

Vibrate with a given effect.

void vibrate(long[] pattern, int repeat, AudioAttributes attributes)

This method was deprecated in API level 26. Use vibrate(VibrationEffect,VibrationAttributes) instead.

Inherited methods

From class java.lang.Object

Object clone()

Creates and returns a copy of this object.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?> getClass()

Returns the runtime class of this Object.

int hashCode()

Returns a hash code value for the object.

final void notify()

Wakes up a single thread that is waiting on this object's monitor.

final void notifyAll()

Wakes up all threads that are waiting on this object's monitor.

String toString()

Returns a string representation of the object.

final void wait(long timeoutMillis, int nanos)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait(long timeoutMillis)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait()

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

Constants

VIBRATION_EFFECT_SUPPORT_NO

public static final int VIBRATION_EFFECT_SUPPORT_NO

Vibration effect support: unsupported

This effect is not natively supported by the underlying hardware, although the system may still play a fallback vibration.

Constant Value: 2 (0x00000002)

VIBRATION_EFFECT_SUPPORT_UNKNOWN

public static final int VIBRATION_EFFECT_SUPPORT_UNKNOWN

Vibration effect support: unknown

The hardware doesn't report its supported effects, so we can't determine whether the effect is supported or not.

Constant Value: 0 (0x00000000)

VIBRATION_EFFECT_SUPPORT_YES

public static final int VIBRATION_EFFECT_SUPPORT_YES

Vibration effect support: supported

This effect is supported by the underlying hardware.

Constant Value: 1 (0x00000001)

Public methods

areAllEffectsSupported

public final int areAllEffectsSupported (int... effectIds)

Query whether the vibrator supports all the given effects. If no argument is provided this method will always return VIBRATION_EFFECT_SUPPORT_YES.

If an effect is not supported, the system may still automatically fall back to a simpler vibration instead, which is not optimised for the specific device, however vibration isn't guaranteed in this case.

If the result is VIBRATION_EFFECT_SUPPORT_YES, all effects in the query are supported by the hardware.

If the result is VIBRATION_EFFECT_SUPPORT_NO, at least one of the effects in the query is not supported, and using them may fall back to an un-optimized vibration or no vibration.

If the result is VIBRATION_EFFECT_SUPPORT_UNKNOWN, the system doesn't know whether all the effects are supported. It may support any or all of the queried effects, but there's no way to programmatically know whether a vibrate(VibrationEffect) call will successfully cause a vibration. It's guaranteed, however, that none of the queried effects are definitively unsupported by the hardware.

Use areEffectsSupported(int) to get individual results for each effect.

Parameters
effectIds int: Which effects to query for.
This value cannot be null.
Value is one of the following:
Returns
int Whether all specified effects are natively supported by the device. Empty query defaults to VIBRATION_EFFECT_SUPPORT_YES.
Value is one of the following:

areEnvelopeEffectsSupported

public boolean areEnvelopeEffectsSupported ()

Checks whether the vibrator supports the creation of envelope effects. Envelope effects are defined by a series of frequency-amplitude pairs with specified transition times, allowing the creation of more complex vibration patterns.

Returns
boolean True if the hardware supports creating envelope effects, false otherwise.

getEnvelopeEffectInfo

public VibratorEnvelopeEffectInfo getEnvelopeEffectInfo ()

Retrieves the vibrator's capabilities and limitations for envelope effects.

These parameters can be used with VibrationEffect.WaveformEnvelopeBuilder to create custom envelope effects.

Returns
VibratorEnvelopeEffectInfo The vibrator's envelope effect information, or null if not supported. If this vibrator is a composite of multiple physical devices then this will return a profile supported in all devices, or null if the intersection is empty or not available.

getFrequencyProfile

public VibratorFrequencyProfile getFrequencyProfile ()

Gets the profile that describes the vibrator output across the supported frequency range.

The profile describes the output acceleration that the device can reach when it vibrates at different frequencies.

Returns
VibratorFrequencyProfile The frequency profile for this vibrator, or null if the vibrator does not have frequency control. If this vibrator is a composite of multiple physical devices then this will return a profile supported in all devices, or null if the intersection is empty or not available.

getId

public int getId ()

Return the ID of this vibrator.

Returns
int A non-negative integer representing the id of the vibrator controlled by this service, or -1 this service is not attached to any physical vibrator.

getQFactor

public float getQFactor ()

Gets the Q factor of the vibrator.

Returns
float the Q factor of the vibrator, or NaN if it's unknown, not applicable, or if this vibrator is a composite of multiple physical devices with different Q factors.

getResonantFrequency

public float getResonantFrequency ()

Gets the resonant frequency of the vibrator, if applicable.

Returns
float the resonant frequency of the vibrator, or NaN if it's unknown, not applicable, or if this vibrator is a composite of multiple physical devices with different frequencies.

hasAmplitudeControl

public abstract boolean hasAmplitudeControl ()

Check whether the vibrator has amplitude control.

Returns
boolean True if the hardware can control the amplitude of the vibrations, otherwise false.

hasVibrator

public abstract boolean hasVibrator ()

Check whether the hardware has a vibrator.

Returns
boolean True if the hardware has a vibrator, else false.

vibrate

public void vibrate (long milliseconds)

This method was deprecated in API level 26.
Use vibrate(VibrationEffect) instead.

Vibrate constantly for the specified period of time.

The app should be in the foreground for the vibration to happen.

.
Requires Manifest.permission.VIBRATE

Parameters
milliseconds long: The number of milliseconds to vibrate.

vibrate

public void vibrate (long[] pattern, 
                int repeat)

This method was deprecated in API level 26.
Use vibrate(VibrationEffect) instead.

Vibrate with a given pattern.

Pass in an array of ints that are the durations for which to turn on or off the vibrator in milliseconds. The first value indicates the number of milliseconds to wait before turning the vibrator on. The next value indicates the number of milliseconds for which to keep the vibrator on before turning it off. Subsequent values alternate between durations in milliseconds to turn the vibrator off or to turn the vibrator on.

To cause the pattern to repeat, pass the index into the pattern array at which to start the repeat, or -1 to disable repeating.

The app should be in the foreground for the vibration to happen.

.
Requires Manifest.permission.VIBRATE

Parameters
pattern long: an array of longs of times for which to turn the vibrator on or off.
repeat int: the index into pattern at which to repeat, or -1 if you don't want to repeat.

vibrate

public void vibrate (long[] pattern, 
                int repeat, 
                AudioAttributes attributes)

This method was deprecated in API level 26.
Use vibrate(VibrationEffect,VibrationAttributes) instead.

Vibrate with a given pattern.

Pass in an array of ints that are the durations for which to turn on or off the vibrator in milliseconds. The first value indicates the number of milliseconds to wait before turning the vibrator on. The next value indicates the number of milliseconds for which to keep the vibrator on before turning it off. Subsequent values alternate between durations in milliseconds to turn the vibrator off or to turn the vibrator on.

To cause the pattern to repeat, pass the index into the pattern array at which to start the repeat, or -1 to disable repeating.

The app should be in the foreground for the vibration to happen. Background apps should specify a ringtone, notification or alarm usage in order to vibrate.

.
Requires Manifest.permission.VIBRATE

Parameters
pattern long: an array of longs of times for which to turn the vibrator on or off.
repeat int: the index into pattern at which to repeat, or -1 if you don't want to repeat.
attributes AudioAttributes: AudioAttributes corresponding to the vibration. For example, specify AudioAttributes.USAGE_ALARM for alarm vibrations or AudioAttributes.USAGE_NOTIFICATION_RINGTONE for vibrations associated with incoming calls.