系统获取Vibrator也是调用Context的getSystemService方法,接下来就可以调用Vibrator的方法控制手机振动了。Vibrator只有三个方法控制手机振动:

1、vibrate(long milliseconds):控制手机振动的毫秒数。

2、vibrate(long[] pattern,int repeat):指定手机以pattern模式振动,例如指定pattern为new long[]{400,800,1200,1600},就是指定在400ms、800ms、1200ms、1600ms这些时间点交替启动、关闭手机振动器,其中repeat指定pattern数组的索引,指定pattern数组中从repeat索引开始的振动进行循环。-1表示只振动一次,非-1表示从pattern的指定下标开始重复振动。

3、cancel():关闭手机振动

下面通过一个示例来演示Vibrator的使用:

Activity:

package com.guyun.vibratortest;import android.app.Activity;import android.os.Bundle;import android.os.Vibrator;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.ToggleButton;public class VibratorTestActivity extends Activity implementsOnCheckedChangeListener {private Vibrator vibrator;private ToggleButton tog1;private ToggleButton tog2;private ToggleButton tog3;private ToggleButton tog4;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_vibrator_test);// 获取系统的Vibrator服务vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);tog1 = (ToggleButton) findViewById(R.id.activity_vibrator_test_tb1);tog2 = (ToggleButton) findViewById(R.id.activity_vibrator_test_tb2);tog3 = (ToggleButton) findViewById(R.id.activity_vibrator_test_tb3);tog4 = (ToggleButton) findViewById(R.id.activity_vibrator_test_tb4);tog1.setOnCheckedChangeListener(this);tog2.setOnCheckedChangeListener(this);tog3.setOnCheckedChangeListener(this);tog4.setOnCheckedChangeListener(this);}@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked) {if (buttonView == tog1) {// 设置震动周期vibrator.vibrate(new long[] { 1000, 10, 100, 1000 }, -1);} else if (buttonView == tog2) {vibrator.vibrate(new long[] { 100, 100, 100, 1000 }, 0);} else if (buttonView == tog3) {vibrator.vibrate(new long[] { 1000, 50, 1000, 50, 1000 }, 0);} else if (buttonView == tog4) {// 设置震动时长vibrator.vibrate(5000);}} else {// 关闭震动vibrator.cancel();}}}


布局XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="单次震动:" />        <ToggleButton            android:id="@+id/activity_vibrator_test_tb1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textOff="开启"            android:textOn="关闭" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="持续震动:" />        <ToggleButton            android:id="@+id/activity_vibrator_test_tb2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textOff="开启"            android:textOn="关闭" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="节奏震动:" />        <ToggleButton            android:id="@+id/activity_vibrator_test_tb3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textOff="开启"            android:textOn="关闭" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="定时长震动:" />        <ToggleButton            android:id="@+id/activity_vibrator_test_tb4"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textOff="开启"            android:textOn="关闭" />    </LinearLayout></LinearLayout>


更多相关文章

  1. Android程序退出彻底关闭进程的方法
  2. Android(安卓)Notification调用测试LED显示
  3. LibGDX输入模块之振荡器
  4. 关闭应用的4种方法
  5. android 情景模式之响铃+震动获取方法
  6. Android仿IOS 仿微信 右滑关闭Activity(附源码)
  7. 自定义栈管理android的Activity
  8. Android的四个组件类型的使用实例
  9. Android设置AlertDialog点击按钮对话框不关闭

随机推荐

  1. Flutter与原生双向通信BasicMessageChann
  2. Android(安卓)与javaEE后台网络链接测试
  3. android:roundIcon和android:icon的区别
  4. Android(安卓)Studio 应用目录结构说明
  5. Android8.1添加MTP数据同步操作
  6. 从J2EE转向Android的第九天-----文件存储
  7. Android通讯:短信
  8. Android系统进程Zygote启动过程的源代码
  9. Android使用DefaultHttpClient访问Wap网
  10. Android(安卓)给自己的类加个事件