The Main.xml code is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:o rientation= "vertical" > <togglebutton android:id= "@+id/btn1" android:layout_width= "Wrap_ Content " android:layout_height=" wrap_content " android:texton=" on " android:textoff=" OFF " android:text= "Short vibration"/> <togglebutton android:id= "@+id/btn2" android:layout_width= "Wrap_ Content " android:layout_height=" wrap_content " android:texton=" on " android:textoff=" OFF " android:text= "Long Vibration"/> <togglebutton android:id= "@+id/btn3" android:layout_width= "WRAP_" Content " android:layout_height=" wrap_content " android:texton=" on " android:textoff=" OFF " android:text= "Rhythm Vibration"/></linearlayout>
. Java code is as follows:
Package Org.lxh.demo;import Android.app.activity;import Android.app.service;import android.os.bundle;import Android.os.vibrator;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.Toast Import Android.widget.togglebutton;public class Hello extends Activity {private ToggleButton btn1 = Null;private Toggleb Utton btn2 = null;private ToggleButton btn3 = null;private Vibrator Myvibrator = null;public void OnCreate (Bundle savedins Tancestate) {super.oncreate (savedinstancestate);//life cycle Method Super.setcontentview (R.layout.main); Set the layout manager to use This.myvibrator = (vibrator) getapplication (). Getsystemservice (service.vibrator_service); this.btn1 = ( ToggleButton) Super.findviewbyid (R.ID.BTN1); this.btn2 = (ToggleButton) Super.findviewbyid (r.id.btn2); this.btn3 = ( ToggleButton) Super.findviewbyid (R.ID.BTN3); This.btn1.setOnClickListener (new Btn1 ()); This.btn2.setOnClickListener (New Btn2 ()); This.btn3.setOnClickListener (new Btn3 ()); Private class BTN1 implements Onclicklistener {public void OnClick (View arg0) {if (btn1.ischecked ()) {Hello.this.myVibrator.vibrate (new long[] {100, 10, 100, 1000} ,-1); Toast.maketext (Hello.this, "short vibration", Toast.length_short). Show (); else {Hello.this.myVibrator.cancel (); Toast.maketext (Hello.this, "Cancel short vibration", Toast.length_short). Show ();}} Private class BTN2 implements Onclicklistener {public void OnClick (View arg0) {if (btn2.ischecked ()) {Hello.this.myVibrat Or.vibrate (new long[] {100, 100, 100, 1000}, 0); Toast.maketext (Hello.this, "long Vibration", toast.length_short). Show (); else {Hello.this.myVibrator.cancel (); Toast.maketext (Hello.this, "Remove long vibration", toast.length_short). Show ();}} Private class Btn3 implements Onclicklistener {public void OnClick (View arg0) {if (btn3.ischecked ()) {Hello.this.myVibrat Or.vibrate (New long[] {1000, 50, 1000, 50}, 0); Toast.maketext (Hello.this, "Rhythm Shaking", toast.length_short). Show (); else {Hello.this.myVibrator.cancel (); Toast.maketext (Hello.this, "Cancel Rhythm Shake", Toast.length_short). Show ();}}}
Configure permissions: <uses-permission android:name= "Android.permission.VIBRATE"/>
Run as follows:
Android--vibrator (Vibration mode)