Android vibration Function
Main Interface code:
Package com. example. vibrator; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class MainActivity extends Activity {private Button btn_vibrator_0; private Button btn_vibrator_1; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btn_vibrator_0 = (Button) findViewById (R. id. btn_vibrator_0); btn_vibrator_1 = (Button) findViewById (R. id. btn_vibrator_1);/*** click to set the default vibration mode, vibrate for 100 milliseconds **/btn_vibrator_0.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stub // stop vibrating VibratorUtil. vibrateCancel (MainActivity. this); VibratorUtil. vibrate (MainActivity. this, 100); // vibrate 100 ms});/*** click to set the custom vibrate mode and duplicate vibrate **/btn_vibrator_1.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stub // stop vibrating VibratorUtil. vibrateCancel (MainActivity. this); // set the vibration parameter, stop for 50 milliseconds, shake for 100 milliseconds long [] prams = {50,100, 50,100}; VibratorUtil. vibrate (MainActivity. this, prams, true); // vibrate 100 ms}) ;}@ Overrideprotected void onDestroy () {// TODO Auto-generated method stubsuper. onDestroy (); // stop VibratorUtil. vibrateCancel (MainActivity. this );}}
Vibration function module:
Package com. example. vibrator; import android. app. activity; import android. app. service; import android. OS. vibrator;/*** mobile phone vibration tool ** @ author Administrator **/public class VibratorUtil {/*** final Activity: the activity instance that calls this method * long milliseconds: the duration of the vibration, in milliseconds */public static void Vibrate (final Activity activity, long milliseconds) {Vibrator vib = (Vibrator) activity. getSystemService (Service. VIBR ATOR_SERVICE); vib. vibrate (milliseconds);}/*** final Activity: the activity instance * long [] pattern that calls this method: Custom vibration mode. The meanings of numbers in the array are [static duration, vibration duration, static duration, and vibration duration]. The unit of duration is millisecond * boolean isRepeat: whether to Vibrate repeatedly. If it is true, it vibrates repeatedly. If it is false, it only shakes once. **/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);}/*** stop vibration **/public static void VibrateCancel (final Activity activity) {Vibrator vib = (Vibrator) activity. getSystemService (Service. VIBRATOR_SERVICE); vib. cancel ();}}
Sample Code address: http://download.csdn.net/detail/stop_pig/8213105