手机震动可以是一种提醒或是替换铃声的事件,如果想要让手机乖乖的震动,需要创建Vibrator对象通过Vibrator方法来实现达到震动的模式,在Vibrator的构造方法中有四个参数前三个值是设置震动的大小,可以把数值改成一大一小这样就可以明显感觉到震动的差异,而最后一个值是震动的时间, Repeat=0时,震动会一直持续repeat=-1震动只会出现一轮,运动完毕后就不再震动
判断ToggleButton是否被开启。如果单击就是“ON”就会启动震动模式如果点击OFF就会关闭振动模式
先定义一个Activity
package cn.hwttnet.com.ui;
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 EX05_06Activity extends Activity {
private Vibrator vbt;
private ToggleButton tb1, tb2, tb3;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vbt = (Vibrator) getApplication().getSystemService(
Service.VIBRATOR_SERVICE);
final ToggleButton tb1 = (ToggleButton) findViewById(R.id.toggleButton1);
final ToggleButton tb2 = (ToggleButton) findViewById(R.id.toggleButton2);
final ToggleButton tb3 = (ToggleButton) findViewById(R.id.toggleButton3);
//短震动
tb1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (tb1.isChecked()) {
vbt.vibrate(new long[] { 100, 10, 100, 1000 }, -1);
Toast.makeText(getApplicationContext(), "震动开启",
Toast.LENGTH_LONG).show();
} else {
vbt.cancel();
Toast.makeText(getApplicationContext(), "震动取消",
Toast.LENGTH_LONG).show();
}
}
});
//长震动
tb2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (tb2.isChecked()) {
vbt.vibrate(new long[] { 100, 100, 100, 1000 }, 0);
Toast.makeText(getApplicationContext(), "震动开启",
Toast.LENGTH_LONG).show();
} else {
vbt.cancel();
Toast.makeText(getApplicationContext(), "震动取消",
Toast.LENGTH_LONG).show();
}
}
});
//节奏震动
tb3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (tb3.isChecked()) {
vbt.vibrate(new long[] { 1000, 50, 1000, 50,1000 }, 0);
Toast.makeText(getApplicationContext(), "震动开启",
Toast.LENGTH_LONG).show();
} else {
vbt.cancel();
Toast.makeText(getApplicationContext(), "震动取消",
Toast.LENGTH_LONG).show();
}
}
});
}
}
定义一个main.xml
<?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:orientation="vertical" >
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:collapseColumns="3"
android:stretchColumns="1" >
<TableRow
android:id="@+id/tablerow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="短震动" />
</TableRow>
<TableRow
android:id="@+id/tablerow2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<ToggleButton
android:id="@+id/toggleButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="长震动" />
</TableRow>
<TableRow
android:id="@+id/tablerow3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<ToggleButton
android:id="@+id/toggleButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="节奏震动" />
</TableRow>
</TableLayout>
</LinearLayout>
然后震动事件必须允许android .permission.VIBARTE权限
<uses-permission android:name="android.permission.VIBRATE"/>
试着把震动的大小做多变化的改变在long[]里面多做一些变化
tb3.vibrate(new long[]{10000,500,10000,500,10000,500,10000},0);