使用android 调用震动的例子
2016-01-28 10:35
489 查看
这两天干了点别的,原因是昨天使用getProviders()方法时,用到了ArrayList这个类,突然想深入的了解他的机制,结果收获非常多,很纠结的看了一位大神分析的JAVA COLLECTION FRAMEWORK的源代码,这样Fail Fast机制就赤裸裸的出现在眼前了,中间核心代码带我走进了设计模式的大门,原来源代码使用了装饰器设计模式,今天自己也是总结了一下学习成果 在 http://blog.csdn.net/octobershiner/article/details/6631997
言归正传,调用Android系统的震动,只需要一个类 那就是Vibrator ,这个类在hard包中,一看系统级的服务,又要通过manifest.xml文件设置权限了
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="uni.vibrator"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".VibratorDemoActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.VIBRATE" />
</manifest> 下面还是一起学习一下SDK吧
Class that operates the vibrator on the device.
If your process exits, any vibration you started with will stop.
//Vibrator类用来操作设备上的震动,如果你的线程退出了,那么启动的震动也会停止
---------------------------------------------------------------------------------------------------------------------------------------------------
public void vibrate (long[] pattern, int repeat)
Since: API Level 1
Vibrate with a given pattern. //根据给定的节奏震动
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.
//传递一个整型数组作为关闭和开启震动的持续时间,以毫秒为单位。第一个值表示等待震动开启的毫秒数,下一个值表示保持震动的毫秒数,这个序列值交替表示震动关闭和开启的毫秒数
To cause the pattern to repeat, pass the index into the pattern array at which to start the repeat, or -1 to disable repeating.
//为了重复的按设定的节奏震动,传递index参数表示重复次数,用-1表示不重复。
Parameters
pattern an array of longs of times for which to turn the vibrator on or off.
repeat the index into pattern at which to repeat, or -1 if you don't want to repeat.
---------------------------------------------------------------------------------------------------------------------------------------------------
还包含一个方法叫做cancel,用来取消震动
一段演示的代码
言归正传,调用Android系统的震动,只需要一个类 那就是Vibrator ,这个类在hard包中,一看系统级的服务,又要通过manifest.xml文件设置权限了
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="uni.vibrator"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".VibratorDemoActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.VIBRATE" />
</manifest> 下面还是一起学习一下SDK吧
Class that operates the vibrator on the device.
If your process exits, any vibration you started with will stop.
//Vibrator类用来操作设备上的震动,如果你的线程退出了,那么启动的震动也会停止
---------------------------------------------------------------------------------------------------------------------------------------------------
public void vibrate (long[] pattern, int repeat)
Since: API Level 1
Vibrate with a given pattern. //根据给定的节奏震动
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.
//传递一个整型数组作为关闭和开启震动的持续时间,以毫秒为单位。第一个值表示等待震动开启的毫秒数,下一个值表示保持震动的毫秒数,这个序列值交替表示震动关闭和开启的毫秒数
To cause the pattern to repeat, pass the index into the pattern array at which to start the repeat, or -1 to disable repeating.
//为了重复的按设定的节奏震动,传递index参数表示重复次数,用-1表示不重复。
Parameters
pattern an array of longs of times for which to turn the vibrator on or off.
repeat the index into pattern at which to repeat, or -1 if you don't want to repeat.
---------------------------------------------------------------------------------------------------------------------------------------------------
还包含一个方法叫做cancel,用来取消震动
一段演示的代码
/* * @author octobershiner * 2011 7 25 * SE.HIT * 一个使用android手机震动的demo * */ package uni.vibrator; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Vibrator; public class VibratorDemoActivity extends Activity { private Vibrator vibrator; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /* * 想设置震动大小可以通过改变pattern来设定,如果开启时间太短,震动效果可能感觉不到 * */ vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); long [] pattern = {100,400,100,400}; // 停止 开启 停止 开启 vibrator.vibrate(pattern,2); //重复两次上面的pattern 如果只想震动一次,index设为-1 } public void onStop(){ super.onStop(); vibrator.cancel(); } }
相关文章推荐
- Android 基于google Zxing实现对手机中的二维码进行扫描
- 基于Android自带插入器的图形波动效果
- android中的进程和线程
- Android Design Support控件之DrawerLayout简单使用
- Android TextUtils类 (字符串相关)
- Android第二天
- Android开发中View.VISIBLE、View.INVISIBLE和View.GONE之间的区别
- 导入adt项目进入android studio报Error:(2) Error parsing XML: prefix must not be bound to one of the reserved
- Android反编译方法
- Android -- Camera2(Android5.0)
- android.hardware.camera2使用指南
- android 调用 dexopt 的位置
- Android 6.0: 动态权限管理的解决方案
- 玩转Android Camera开发(一):Surfaceview预览Camera,基础拍照功能完整demo
- Android常见错误:Jar mismatch! Fix your dependencies
- Android 开源框架ViewPageIndicator 和 ViewPager 仿网易新闻客户端Tab标签
- Android:解决RadioGroup中RadioButton的图片自定义及每项间隔距离一样
- Android硬件抽象层(HAL)概要介绍和学习计划
- 收藏左侧菜单
- 【已解决】Android Studio无法识别虚拟机,虚拟机无法运行