Android手機中的震動由Vibrator實現。設置震動事件,需要知道其震動的時間長短、震動的週期等。
在Android Vibrator中,震動的時間一毫秒計算(1/1000秒),所以如果設置的時間值太小,會感覺不出來。
通過調用Vibrator的vibrate(long[] pattern, int repeat)方法實現。
前一個參數爲設置震動的效果的數組,第二個參數爲 -1表示只震動一次,爲0則震動會一直持續。
java:
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.os.Vibrator;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
private Vibrator vibrator;
private ToggleButton tog1, tog2, tog3;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
tog1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
vibrator.vibrate(new long[] { 1000, 10, 100, 1000 }, -1);
showtoast("OK");
} else {
vibrator.cancel();
showtoast("CANCEL");
}
}
});
tog2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
vibrator.vibrate(new long[] { 100, 100, 100, 1000 }, 0);
showtoast("OK");
} else {
vibrator.cancel();
showtoast("CANCEL");
}
}
});
tog3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
vibrator.vibrate(new long[] { 1000, 500, 1000, 500 }, 0);
showtoast("OK");
} else {
vibrator.cancel();
showtoast("CANCEL");
}
}
});
}
private void init() {
tog1 = (ToggleButton) findViewById(R.id.toggle1);
tog2 = (ToggleButton) findViewById(R.id.toggle2);
tog3 = (ToggleButton) findViewById(R.id.toggle3);
vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
}
private void showtoast(String msg) {
Toast.makeText(this, msg, 1).show();
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
/>
<TextView
android:id="@+id/text2"
android:layout_width="127px"
android:layout_height="35px"
android:layout_x="90px"
android:layout_y="33px"
android:text="短時間"
/>
<TextView
android:id="@+id/text3"
android:layout_width="127px"
android:layout_height="35px"
android:layout_x="90px"
android:layout_y="115px"
android:text="長時間"
/>
<TextView
android:id="@+id/text4"
android:layout_width="127px"
android:layout_height="35px"
android:layout_x="90px"
android:layout_y="216px"
android:text="節奏感"
/>
<ToggleButton
android:id="@+id/toggle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="29px"
android:layout_y="31px"
/>
<ToggleButton
android:id="@+id/toggle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="29px"
android:layout_y="114px"
/>
<ToggleButton
android:id="@+id/toggle3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="29px"
android:layout_y="214px"
/>
</AbsoluteLayout>
最後別忘了加上:<uses-permission android:name="android.permission.VIBRATE"/>