Android手机中的震动由Vibrator实现。设置震动事件,需要知道其震动的时间长短、震动的周期等。
在Android Vibrator中,震动的时间一毫秒计算(1/1000秒),所以如果设置的时间值太小,会感觉不出来。
通过调用Vibrator的vibrate(long[] pattern, int repeat)方法实现。
3)记得添加权限在AndroidManifest.xml中添加代码如下
在Android Vibrator中,震动的时间一毫秒计算(1/1000秒),所以如果设置的时间值太小,会感觉不出来。
通过调用Vibrator的vibrate(long[] pattern, int repeat)方法实现。
前一个参数为设置震动的效果的数组,第二个参数为 -1表示只震动一次,为0则震动会一直持续。
1)修改activity_main.xml代码如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="短震动" />
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="长震动" />
<ToggleButton
android:id="@+id/toggleButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="节奏震动" />
<ToggleButton
android:id="@+id/toggleButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton" />
</LinearLayout>
</LinearLayout>
package com.example.androidapp;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
private ToggleButton toggleButton1=null;
private ToggleButton toggleButton2=null;
private ToggleButton toggleButton3=null;
private Vibrator vibrator=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vibrator=(Vibrator)this.getSystemService(VIBRATOR_SERVICE);
toggleButton1=(ToggleButton)findViewById(R.id.toggleButton1);
toggleButton2=(ToggleButton)findViewById(R.id.toggleButton2);
toggleButton3=(ToggleButton)findViewById(R.id.toggleButton3);
/*短震动*/
toggleButton1.setOnCheckedChangeListener(new ToggleButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
//设置震动周期,第二个参数为 -1表示只震动一次
vibrator.vibrate(new long[]{1000, 10, 100, 1000},-1);
}else{
//取消震动
vibrator.cancel();
}
}
});
/*长震动*/
toggleButton2.setOnCheckedChangeListener(new ToggleButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//设置震动周期,第二个参数为0则震动会一直持续
if(isChecked){
vibrator.vibrate(new long[] { 100, 100, 100, 1000 }, 0);
}
else {
//取消震动
vibrator.cancel();
}
}
});
/*周期震动*/
toggleButton3.setOnCheckedChangeListener(new ToggleButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
//设置震动周期
vibrator.vibrate(new long[] { 1000, 50, 1000, 50, 1000 }, 0);
}
else {
//取消震动
vibrator.cancel();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
3)记得添加权限在AndroidManifest.xml中添加代码如下
<uses-permission android:name="android.permission.VIBRATE"/>