Heartbeat vibration Vibrator Used, heartbeat vibration vibrator
The device vibrator can be used to set the vibration frequency to simulate the heartbeat vibration.
There are two ways to obtain the device vibrator:
Vibrator = (Vibrator) getSystemService (VIBRATOR_SERVICE );
Vibrator = (Vibrator) getApplicationContext (). getSystemService (Service. VIBRATOR_SERVICE );
There are two ways to enable vibration setting:
First:
Vibrator. vibrate (1000); // directly vibrate for 1000 milliseconds without waiting time
Second:
Use an array to simulate the vibration frequency.
Long [] pattern = {50, 30, 60, 30}; // {Wait time, shake time, wait time, shake time ...}
Vibrator. vibrate (pattern,-1); //-1 is unique, and non-1 is repeated starting from the specified subscript of pattern
Parameter for the first time: pattern: Specifies the vibration frequency. Wait for 50 milliseconds, shake for 30 milliseconds, wait for 60 milliseconds, shake for 30 milliseconds ...... continue to vibrate at this frequency
The second parameter: <0: it is usually set to-1, indicating that the system will vibrate only once at the specified frequency. Wait for 50 milliseconds and shake for 30 milliseconds.
It must be greater than or equal to 0, but cannot be greater than or equal to pattern. length, that is, 0 <= X <pattern. length. Otherwise, an exception occurs in java. lang. ArrayIndexOutOfBoundsException.
Cancel vibration:
Vibrator. cancel ();
At last, I forgot to add permissions to the AndroidManifest. xml file:
<Uses-permission android: name = "android. permission. VIBRATE"/>