Vibrator控制手机震动
2015-08-26 14:33
316 查看
Vibrator控制手机震动
效果图
源码
下载地址(Android Studio工程):http://download.csdn.net/detail/q4878802/9049755添加权限
<!-- 震动的权限 --> <uses-permission android:name="android.permission.VIBRATE" />
工具类
package com.kongqw.kqwvibrator.engine; import android.content.Context; import android.os.Vibrator; /** * Created by kongqw on 2015/8/26. */ public class KqwVibrator { private static KqwVibrator mKqwVibrator; private Context mContext; private Vibrator mVibrator; private KqwVibrator(Context context) { mContext = context; // 初始化振动器对象 mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); } public static KqwVibrator getInstance(Context context) { if (null == mKqwVibrator) { mKqwVibrator = new KqwVibrator(context); } return mKqwVibrator; } /** * 开始震动 * * @param time 震动时间 毫秒 */ public void vibrate(long time) { mVibrator.vibrate(time); } /** * 取消震动 */ public void cancle() { mVibrator.cancel(); } /** * 重复震动 */ public void repeatVibrate() { //等待1秒,震动2秒,等待1秒,震动3秒 long[] pattern = {1000, 2000, 1000, 3000}; //-1表示不重复, 如果不是-1, 比如改成1, 表示从前面这个long数组的下标为1的元素开始重复. mVibrator.vibrate(pattern, -1); } }
测试类
package com.kongqw.kqwvibrator; import android.app.Activity; import android.os.Bundle; import android.view.View; import com.kongqw.kqwvibrator.engine.KqwVibrator; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 开始震动500毫秒 * * @param view */ public void button1(View view) { KqwVibrator.getInstance(this).vibrate(500); } /** * 重复震动 * * @param view */ public void button2(View view) { KqwVibrator.getInstance(this).repeatVibrate(); } /** * 停止震动 * * @param view */ public void button3(View view) { KqwVibrator.getInstance(this).cancle(); } }
页面布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:onClick="button1" android:text="开始震动500毫秒" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/button1" android:onClick="button2" android:text="重复震动" /> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/button2" android:onClick="button3" android:text="停止震动" /> </RelativeLayout>
说明
mVibrator.vibrate(pattern, -1);
这个是一个间歇性震动的方法,第一个参数是一个long类型的数组(毫秒),单数(双数角标)为等待时间,双数(单数角标)为震动时间。
例:
//等待1秒,震动2秒,等待1秒,震动3秒 long[] pattern = {1000, 2000, 1000, 3000};
第二个参数是循环的参数,是几就表示下次从第几个角标开始循环,-1表示不循环。
父类方法
/** * Vibrate with a given pattern. * * <p> * Pass in an array of ints that are the durations for which to turn on or off * the vibrator in milliseconds. The first value indicates the number of milliseconds * to wait before turning the vibrator on. The next value indicates the number of milliseconds * for which to keep the vibrator on before turning it off. Subsequent values alternate * between durations in milliseconds to turn the vibrator off or to turn the vibrator on. * </p><p> * To cause the pattern to repeat, pass the index into the pattern array at which * to start the repeat, or -1 to disable repeating. * </p> * <p>This method requires the caller to hold the permission * {@link android.Manifest.permission#VIBRATE}. * * @param pattern an array of longs of times for which to turn the vibrator on or off. * @param repeat the index into pattern at which to repeat, or -1 if * you don't want to repeat. */ public void vibrate(long[] pattern, int repeat) { vibrate(pattern, repeat, null); } /** * Vibrate with a given pattern. * * <p> * Pass in an array of ints that are the durations for which to turn on or off * the vibrator in milliseconds. The first value indicates the number of milliseconds * to wait before turning the vibrator on. The next value indicates the number of milliseconds * for which to keep the vibrator on before turning it off. Subsequent values alternate * between durations in milliseconds to turn the vibrator off or to turn the vibrator on. * </p><p> * To cause the pattern to repeat, pass the index into the pattern array at which * to start the repeat, or -1 to disable repeating. * </p> * <p>This method requires the caller to hold the permission * {@link android.Manifest.permission#VIBRATE}. * * @param pattern an array of longs of times for which to turn the vibrator on or off. * @param repeat the index into pattern at which to repeat, or -1 if * you don't want to repeat. * @param attributes {@link AudioAttributes} corresponding to the vibration. For example, * specify {@link AudioAttributes#USAGE_ALARM} for alarm vibrations or * {@link AudioAttributes#USAGE_NOTIFICATION_RINGTONE} for * vibrations associated with incoming calls. */ public void vibrate(long[] pattern, int repeat, AudioAttributes attributes) { vibrate(Process.myUid(), mPackageName, pattern, repeat, attributes); }
相关文章推荐
- 2012989 - IDES ERP 6.0 incl. EHP7
- 跟我一起写Makefile:书写命令
- Vibrator控制手机震动
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
- ImageView 加大点击范围
- 通过httpclient向服务器传送数据并获得返回值
- poj_2154
- 简单的makefile
- Android启动过程深入解析
- tabhost设置动画
- Oracle 常用函数的用法
- java类初始化顺序
- yii2 去掉index.php的方法
- js 备忘
- iOS 截屏方法
- 设计模式-观察者模式
- 【Leetcode】Rotate Image
- 关于linux socket 编程 端口复用的理解
- 有关虚函数的覆盖 和 子类中函数的隐藏
- [leetcode-208]Implement Trie (Prefix Tree)(java)