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 );