Use of Android-Vibrator

Source: Internet
Author: User

The vibration in the Android mobile phone is implemented by vibrator. To set a vibration event, you need to know the duration and cycle of the vibration.

In Android, the vibration time is calculated in one millisecond (1/1000 seconds). Therefore, if the set time value is too small, it cannot be felt.

Call vibrate (long [] pattern, int repeat) of vibrator.

The previous parameter is an array of effects for vibration. The second parameter is-1, indicating that only one vibration occurs. If it is 0, the vibration will continue.

A demo:

Package COM. shao. vibrator; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. OS. vibrator; <br/> Import android. widget. compoundbutton; <br/> Import android. widget. toast; <br/> Import android. widget. compoundbutton. oncheckedchangelistener; <br/> Import android. widget. togglebutton; </P> <p> public class vibratoracti1_extends activity {<br/>/** called when the activity is first created. */</P> <p> private vibrator; <br/> private togglebutton tog1; <br/> private togglebutton tog2; <br/> private togglebutton tog3; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> Init (); <br/> tog1.setoncheckedchangelistener (New oncheckedchangelistener () {</P> <p> @ override <br/> Public void oncheckedchanged (compoundbutton buttonview, Boolean ischecked) {<br/> // todo auto-generated method stub <br/> If (ischecked) {<br/> // set the vibration cycle <br/> vibrator. vibrate (New long [] {1000,10, 100,1000},-1); <br/> showtoast ("OK "); <br/>} else {<br/> // cancel the vibration <br/> vibrator. cancel (); <br/> showtoast ("cancel"); <br/>}< br/> }); <br/> tog2.setoncheckedchangelistener (New oncheckedchangelistener () {</P> <p> @ override <br/> Public void oncheckedchanged (compoundbutton buttonview, Boolean ischecked) {<br/> // todo auto-generated method stub <br/> If (ischecked) {<br/> // set the vibration cycle <br/> vibrator. vibrate (New long [] {100,100,100,100 0}, 0); <br/> showtoast ("OK "); <br/>} else {<br/> // cancel the vibration <br/> vibrator. cancel (); <br/> showtoast ("cancel"); <br/>}< br/> }); <br/> tog3.setoncheckedchangelistener (New oncheckedchangelistener () {</P> <p> @ override <br/> Public void oncheckedchanged (compoundbutton buttonview, Boolean ischecked) {<br/> // todo auto-generated method stub <br/> If (ischecked) {<br/> // set the vibration cycle <br/> vibrator. vibrate (New long [] {1000, 50,}, 0); <br/> showtoast ("OK "); <br/>} else {<br/> // cancel the vibration <br/> vibrator. cancel (); <br/> showtoast ("cancel"); <br/>}< br/> }); </P> <p >}< br/> private void Init () {<br/> tog1 = (togglebutton) findviewbyid (R. id. tog1); <br/> tog2 = (togglebutton) findviewbyid (R. id. tog2); <br/> tog3 = (togglebutton) findviewbyid (R. id. tog3); <br/> vibrator = (vibrator) This. getsystemservice (vibrator_service); <br/>}< br/> private void showtoast (string MSG) {<br/> toast. maketext (this, MSG, 1 ). show (); <br/>}< br/>}
XML:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <relativelayout <br/> Android: layout_margintop = "20dp" <br/> Android: Orientation = "horizontal" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content"> <br/> <textview <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "Short vibration" <br/> <togglebutton <br/> Android: Id = "@ + ID/tog1" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: texton = "disabled" <br/> Android: textoff = "open" <br/> Android: layout_alignparentright = "true" <br/> </relativelayout> <br/> <relativelayout <br/> Android: layout_margintop = "20dp" <br/> Android: Orientation = "horizontal" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content"> <br/> <textview <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" </P> <p> Android: text = "Long vibration" <br/> <togglebutton <br/> Android: id = "@ + ID/tog2" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: texton = "close" <br/> Android: textoff = "open" <br/> Android: layout_alignparentright = "true" <br/> </relativelayout> <br/> <relativelayout <br/> Android: layout_margintop = "20dp" <br/> Android: Orientation = "horizontal" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content"> <br/> <textview <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" </P> <p> Android: text = "rhythm vibration" <br/> <togglebutton <br/> Android: id = "@ + ID/tog3" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: texton = "close" <br/> Android: textoff = "open" <br/> Android: layout_alignparentright = "true" <br/> </relativelayout> <br/> </linearlayout> <br/>

Do not forget to add the <uses-Permission Android: Name = "android. Permission. Vibrate"/> permission

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.