Brief instructions on setting up of Vibrator for mobile phone vibration in Android

  • 2020-05-09 19:15:51
  • OfStack

Steps for setting up mobile phone vibration in Android (Vibrator) :
a, get mobile phone vibration service through system service, Vibrator vibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);

b. Check whether vibrator exists after receiving the vibration service:
vibrator.hasVibrator();
Checks if the current hardware has vibrator, returns true if it does, and false if it does not.

c, make appropriate calls according to actual needs,
vibrator.vibrate(long milliseconds);
Start vibrator for milliseconds milliseconds.
vibrator.vibrate(long[] pattern, int repeat);
Repeat repeat for vibrator startup in pattern mode. (pattern the form of a new long [] {arg1 arg2, arg3, arg4... }, in which two groups of 1, arg1 and arg2 are 1 group, arg3 and arg4 are 1 group, the first one of each group represents the number of milliseconds to wait to start vibrator, the last one represents the number of milliseconds to stop vibrator, and then reciprocate. Repeat refers to the number of repetitions, and when it is -1, it means that it is not repeated and only runs once in the square of pattern).
vibrator.cancel();
Vibrator stop.

Note: the following permissions are required to use the vibration service: android.permission.VIBRATE.

Related articles: