Android mobile phone Vibrator, andoridvibrator

Source: Internet
Author: User

Android mobile phone Vibrator, andoridvibrator

Tags: androidvibrator, vibrator, it Category: Andorid

Obtain the Vibrator instance: Vibrator mVibrator = (Vibrator) context. getSystemService (Context. VIBRATOR_SERVICE); Vibrator. vibrate () method: when there is only one parameter, the first parameter is used to specify the number of milliseconds of vibration. When two parameters are required, 1st parameters are used to specify the sample of the vibration time, and 2nd parameters are used to specify whether a cycle is required. The vibration time sample is an array specified by the interaction between the vibration time and the wait time.

※In the following example, wait for 3 seconds after the program starts, vibrate for 1 second, wait for 2 seconds, vibrate for 5 seconds, and then wait for 3 seconds to vibrate for 1 second.
Long [] pattern = {3000,100 0, 2000,500 0, 3000,100 0}; // OFF/ON/OFF/ON...

You need to set the following classes in AndroidManifest. xml.
<Uses-permission android: name = "android. permission. VIBRATE"/>

Routine source code (Java)
Public class VibratorTestActivity extends Activity {

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Vibrator vibrator = (Vibrator) getSystemService (VIBRATOR_SERVICE );

Long [] pattern = {3000,100 0, 2000,500 0, 3000,100 0}; // OFF/ON/OFF/ON

Vibrator. vibrate (pattern,-1 );

}

@ Override
Public boolean onTouchEvent (MotionEvent event ){
If (event. getAction () = MotionEvent. ACTION_MOVE ){
Vibrator vibrator = (Vibrator) getSystemService (VIBRATOR_SERVICE );
Vibrator. vibrate (10 );
}
Return super. onTouchEvent (event );

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.