Android--vibrator (Vibration mode)

Source: Internet
Author: User

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)

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.