Package cn.com. chenzheng_java; </P> <p> Import android. app. activity; <br/> Import android. media. audiomanager; <br/> Import android. OS. bundle; <br/> Import android. OS. vibrator; <br/> Import android. view. view; <br/> Import android. widget. toast; <br/> Import android. widget. togglebutton; </P> <p> public class vibrateactivity extends activity {</P> <p> private togglebutton button; <br/> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> // todo auto-generated method stub <br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. vibrate); </P> <p> button = (togglebutton) findviewbyid (R. id. togglebutton1); <br/> button. setonclicklistener (new view. onclicklistener () {</P> <p> @ override <br/> Public void onclick (view v) {<br/> Boolean flag = button. ischecked (); <br/> If (FLAG) {<br/> // obtain the vibrate object <br/> vibrator = (vibrator) getsystemservice (vibrator_service ); <br/> // vibrator. vibrate (3000); <br/> // set the vibrate vibration cycle <br/> vibrator. vibrate (New long [] {1000,2000, 3000,4000}, 0); <br/> toast. maketext (vibrateactivity. this, "shaken", toast. length_long ). show (); <br/>}< br/> else {</P> <p >}< br/> }); </P> <p >}< br/>
Android. OS. vibrate represents the mobile phone vibrator. The usage is very simple. Get the example using the getsystemservice () method, and then call the vibrate method to implement vibration.
To use this vibrator, you must specify the permission in androidmanifest. xml <uses-Permission Android: Name = "android. Permission. Vibrate"> </uses-Permission>