Android vibration Function

Source: Internet
Author: User

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

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.