android手機中的震動由Vibrator實現。設定震動事件,需要知道其震動的時間長短、震動的周期等。
在android中,震動的時間一毫秒計算(1/1000秒),所以如果設定的時間值太小,會感覺不出來。
通過調用Vibrator的vibrate(long[] pattern, int repeat)方法實現。
前一個參數為設定震動的效果的數組,第二個參數為 -1表示只震動一次,為0則震動會一直持續。
一個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 VibratorActivity extends Activity {<br /> /** Called when the activity is first created. */</p><p>private Vibrator 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 />//設定震動周期<br />vibrator.vibrate(new long[]{1000,10,100,1000}, -1);<br />showToast("OK");<br />}else{<br />//取消震動<br />vibrator.cancel();<br />showToast("CANCEL");<br />}<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 />//設定震動周期<br />vibrator.vibrate(new long[]{100,100,100,1000}, 0);<br />showToast("OK");<br />}else{<br />//取消震動<br />vibrator.cancel();<br />showToast("CANCEL");<br />}<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 />//設定震動周期<br />vibrator.vibrate(new long[]{1000,50,1000,50,1000}, 0);<br />showToast("OK");<br />}else{<br />//取消震動<br />vibrator.cancel();<br />showToast("CANCEL");<br />}<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 /> ><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="短震動"<br /> /><br /><ToggleButton<br />android:id="@+id/tog1"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:textOn="關閉"<br /> android:textOff="開啟"<br /> android:layout_alignParentRight="true"<br /> /><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="長震動"<br /> /><br /><ToggleButton<br />android:id="@+id/tog2"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:textOn="關閉"<br /> android:textOff="開啟"<br /> android:layout_alignParentRight="true"<br /> /><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="節奏震動"<br /> /><br /><ToggleButton<br />android:id="@+id/tog3"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:textOn="關閉"<br /> android:textOff="開啟"<br /> android:layout_alignParentRight="true"<br /> /><br /> </RelativeLayout><br /></LinearLayout><br />
最後別忘了加上 <uses-permission android:name="android.permission.VIBRATE"/>許可權