Heartbeat vibration Vibrator Used, heartbeat vibration vibrator

Source: Internet
Author: User

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"/>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.