Detailed explanation of Android Vibrator mobile phone vibration

Keywords: Android xml

add permission

Add the following shake permissions to the AndroidManifest.xml file

<uses-permission android:name="android.permission.VIBRATE"/> 

Get Vibrator

vibrator = (Vibrator)context.getSystemService(this.VIBRATOR_SERVICE);

Simple vibration

First look at the source code of the simple vibration method vibrate(long milliseconds)

/**
 * Vibrate constantly for the specified period of time.
 *
 * @param milliseconds The number of milliseconds to vibrate.
 *
 * @deprecated Use {@link #vibrate(VibrationEffect)} instead.
 */
@Deprecated
@RequiresPermission(android.Manifest.permission.VIBRATE)
public void vibrate(long milliseconds) {
	vibrate(milliseconds, null);
}

The simple way to use is to send in a millisecond value, and the phone will continue to vibrate the millisecond value for a long time.

vibrator = (Vibrator)context.getSystemService(this.VIBRATOR_SERVICE);
vibrator.vibrate(1000); // Shake for one second

Complex vibration

First, look at the source code and annotations of the complex vibration method vibrate(long[] pattern, int repeat)

/**
 * Vibrate with a given pattern.
 *
 * <p>
 * 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.
 * </p><p>
 * To cause the pattern to repeat, pass the index into the pattern array at which
 * to start the repeat, or -1 to disable repeating.
 * </p>
 *
 * @param pattern an array of longs of times for which to turn the vibrator on or off.
 * @param repeat the index into pattern at which to repeat, or -1 if
 *        you don't want to repeat.
 *
 * @deprecated Use {@link #vibrate(VibrationEffect)} instead.
 */
@Deprecated
@RequiresPermission(android.Manifest.permission.VIBRATE)
public void vibrate(long[] pattern, int repeat) {
	vibrate(pattern, repeat, null);
}
  1. The first parameter, pattern, represents the mode of vibration. It is a long array, which represents the time of vibration and stopping vibration alternately, and the first is the duration of stopping vibration, the second is the duration of vibration, and the third is the duration of stopping vibration, and so on.
  2. The second parameter repeat represents an infinite repetition of vibration starting from the number of positions in the pattern, and if set to -1, it means no repetition of vibration.
vibrator = (Vibrator)context.getSystemService(this.VIBRATOR_SERVICE);
long[] pattern = {100, 200, 100, 200}; // After 100 ms, start shaking 200 ms, then stop 100 ms, then shake 200 ms.
vibrator.vibrate(pattern, 0); // Repeated cyclic vibration from position 0 of pattern

If set to repetitive vibration, cancel method should be called to stop cyclic vibration.

vibrator.cancel();

 

Posted by ayampanggang on Sun, 06 Oct 2019 04:34:55 -0700