Android mobile phone vibration function implementation

Source: Internet
Author: User

In interaction with users, the vibration function is often used to remind users. This function is easy to implement. See the following code:


[Java] import android. app. Activity;
Import android. app. Service;
Import android. OS. Vibrator;
 
Public class TipHelper {
Public static void Vibrate (final Activity activity, long milliseconds ){
Vibrator vib = (Vibrator) activity. getSystemService (Service. VIBRATOR_SERVICE );
Vib. vibrate (milliseconds );
}
Public static void Vibrate (final Activity activity, long [] pattern, boolean isRepeat ){
Vibrator vib = (Vibrator) activity. getSystemService (Service. VIBRATOR_SERVICE );
Vib. vibrate (pattern, isRepeat? 1:-1 );
}
}
Import android. app. Activity;
Import android. app. Service;
Import android. OS. Vibrator;

Public class TipHelper {
Public static void Vibrate (final Activity activity, long milliseconds ){
Vibrator vib = (Vibrator) activity. getSystemService (Service. VIBRATOR_SERVICE );
Vib. vibrate (milliseconds );
}
Public static void Vibrate (final Activity activity, long [] pattern, boolean isRepeat ){
Vibrator vib = (Vibrator) activity. getSystemService (Service. VIBRATOR_SERVICE );
Vib. vibrate (pattern, isRepeat? 1:-1 );
}
}
 

 


Of course, www.2cto.com only has the above Code. We also need to add the vibration permission to AndroidManifest. xml:


[Html] <uses-permission android: name = "android. permission. VIBRATE"/>
<Uses-permission android: name = "android. permission. VIBRATE"/>
Through the above operation, we can use the function defined by TipHelper. The parameters of the two Vibrate functions are described as follows:

Final Activity: the activity instance that calls this method

Long milliseconds: the duration of the vibration, in milliseconds


Long [] pattern: Custom vibration mode. The meanings of numbers in the array are [static duration, vibration duration, static duration, and vibration duration]. The duration is measured in milliseconds.


Boolean isRepeat: whether to vibrate repeatedly. If it is true, it shakes repeatedly. If it is false, it only shakes once.


From the pure soul

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.