Android--Vibrator实现手机震动效果
手机的震动功能相信大家都不会陌生,现在就让我们解读手机的震动。
其实,要实现手机的震动并不难,只需要实现一个类,并调用其中的方法,设定相应的参数即可。
下面给出介绍:
这段文档来自Google SDK文档
Class that operates the vibrator on the device.
If your process exits, any vibration you started with will stop.
To obtain an instance of the system vibrator, call getSystemService(String)
with VIBRATOR_SERVICE
as argument.
我们通常实现的方法如下:
public abstract void vibrate (long[] pattern, int repeat)
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.
This method requires the caller to hold the permission VIBRATE
.
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. |
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><!-- 建立第一個TextView --><TextView android:id="@+id/myTextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello"/><!-- 建立第二個TextView --><TextViewandroid:id="@+id/myTextView2"android:layout_width="127px"android:layout_height="35px"android:layout_x="90px"android:layout_y="33px"android:text="@string/str_text1"/><!-- 建立第三個TextView --><TextViewandroid:id="@+id/myTextView3"android:layout_width="127px"android:layout_height="35px"android:layout_x="90px"android:layout_y="115px"android:text="@string/str_text2"/><!-- 建立第四個TextView --><TextViewandroid:id="@+id/myTextView4"android:layout_width="127px"android:layout_height="35px"android:layout_x="90px"android:layout_y="216px"android:text="@string/str_text3"/><!-- 建立第一個ToggleButton --> <ToggleButtonandroid:id="@+id/myTogglebutton1"android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_x="29px"android:layout_y="31px" /><!-- 建立第二個ToggleButton --> <ToggleButtonandroid:id="@+id/myTogglebutton2"android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_x="29px"android:layout_y="114px" /><!-- 建立第三個ToggleButton --> <ToggleButtonandroid:id="@+id/myTogglebutton3"android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_x="29px"android:layout_y="214px"/>
</AbsoluteLayout>
2.主程序的代码
public class EX05_06 extends Activity
{ private Vibrator mVibrator01;/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(R.layout.main);/*设定ToggleButton的对象*/mVibrator01 = ( Vibrator )getApplication().getSystemService( Service .VIBRATOR_SERVICE ); final ToggleButton mtogglebutton1 = (ToggleButton) findViewById(R.id .myTogglebutton1); final ToggleButton mtogglebutton2 = (ToggleButton) findViewById(R.id .myTogglebutton2); final ToggleButton mtogglebutton3 = (ToggleButton) findViewById(R.id .myTogglebutton3); /*设定ToggleButton使用OnClickListener来启动事件*/mtogglebutton1.setOnClickListener(new OnClickListener(){ public void onClick(View v) { if (mtogglebutton1.isChecked()) { // public abstract void vibrate (long[] pattern, int repeat)
// Added in 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.
//
// This method requires the caller to hold the permission VIBRATE.
//
// 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. /*设定震动的周期*/mVibrator01.vibrate( new long[]{100,10,100,1000},-1); /*用Toast显示震动启动*/ Toast.makeText(EX05_06.this, getString(R.string.str_ok) ,Toast.LENGTH_SHORT).show();}else{ /*取消震动*/ mVibrator01.cancel(); /*用Toast显示震动取消*/ Toast.makeText(EX05_06.this, getString(R.string.str_end) ,Toast.LENGTH_SHORT).show();} } });/*设定ToggleButton使用OnClickListener来启动事件*/ mtogglebutton2.setOnClickListener(new OnClickListener(){ public void onClick(View v) { if (mtogglebutton2.isChecked()){
/*设定震动的周期*/ mVibrator01.vibrate( new long[]{100,100,100,1000},0);
/*用Toast显示震动启动*/
Toast.makeText(EX05_06.this, getString(R.string.str_ok) ,Toast.LENGTH_SHORT).show();
}else{ /*取消震动*/mVibrator01.cancel(); /*用Toast显示震动取消*/ Toast.makeText(EX05_06.this, getString(R.string.str_end) ,Toast.LENGTH_SHORT).show();}} });mtogglebutton3.setOnClickListener(new OnClickListener(){ public void onClick(View v) { if (mtogglebutton3.isChecked()) {/*设定震动的周期*/mVibrator01.vibrate( new long[]{1000,50,1000,50,1000},0); /*用Toast显示震动启动*/Toast.makeText(EX05_06.this, getString(R.string.str_ok) ,Toast.LENGTH_SHORT).show();}else{ /*取消震动*/ mVibrator01.cancel(); /*用Toast显示震动取消*/Toast.makeText(EX05_06.this, getString(R.string.str_end) ,Toast.LENGTH_SHORT).show();}} }); } }
Android--Vibrator实现手机震动效果相关推荐
- android 手机震动功能吗,Android编程实现手机震动功能的方法
本文实例讲述了android编程实现手机震动功能的方法.分享给大家供大家参考,具体如下: 在与用户交互时,常常会用到震动功能,以提醒用户.该功能实现比较简单,请参阅下面主要代码: import and ...
- 控制Android充电震动的代码,Android编程实现手机震动功能的方法
本文实例讲述了Android编程实现手机震动功能的方法.分享给大家供大家参考,具体如下: 在与用户交互时,常常会用到震动功能,以提醒用户.该功能实现比较简单,请参阅下面主要代码: import and ...
- java手机振动软件_Android实现手机震动效果
本文实例介绍了Android实现手机震动.抖动效果,分享给大家供大家参考,具体内容如下 (1)布局文件如下 xmlns:tools="http://schemas.android.com/t ...
- 震屏效果java_CocosCreator 实现手机震动效果
IOS 震动的写法#import @interface JSC : NSObject + (void) ddd1; + (void) ddd2; + (void) ddd3; + (void) ddd ...
- 测试新版Android Studio的手机镜像效果
学更好的别人, 做更好的自己. --<微卡智享> 本文长度为669字,预计阅读2分钟 前言 春节刚上班,就开始了疯狂出差的节奏,期间发现Android Studio发布新的版本2022.1 ...
- Android中关于手机震动
1.获取振动器Vibrator的实例: Vibrator vibrator = (Vibrator)getApplication().getSystemService(Service.VIBRATOR ...
- Android开发:手机震动工具类
新思路,如果你在做关于通知Notification方便的工作,在涉及到多种通知方式组合时(例如:铃声.轻震动.强震动等),感觉到系统提供的API比较吃力的话,建议可以自己来实现通知效果,根据开发经验, ...
- H5移动端实现手机震动效果
判断兼容 浏览器对振动API的支持情况,一个好的习惯就是在使用之前要检查一下当前你的应用环境.浏览器是否支持振动API.下面就是检测的方法: setTimeout(()=>{navigator. ...
- Android 如何实现手机震动
一.Android的震动实现--Vibrator类 在开发过程中,需要实现震动,网上有好些例子,这里就不阐述了,直接上code. 一定要在AndroidManifest.xml增加权限: <us ...
最新文章
- c++控制台应用每一列数据如何对齐_Python数据分析第五节 pandas入门
- 【Dlib】dlib和opencv的互转
- 配置cisco路由器特定时间重启
- 网站建设ASP中UTF-8与GB2312编码转换乱码问题的解决方法
- c/c++ typedef定义函数指针(Hook前奏2)
- [spring]Attribute scope must be declared for element type bean
- 深度学习(八)caffe源码学习-未完待续
- cuSPARSE库:(一)函数的异步执行
- Python爬虫系列:判断目标网页编码的几种方法
- JSK-19 加一【入门】
- WPF中的TreeView入门
- 手持式频谱分析仪怎么选择
- ES3、ES5、ES6继承
- python 使用 requests 库发送请求及设置代理
- 最近三次蓝屏Bluescreen STOP 0x000000ea
- POI根据模板导出word文件,以及word转PDF,PDF转图片再插入PDF中(防止PDF被修改)
- ACMoi蓝桥杯刷题网站推荐
- 银行木马卷土重来、开发者破坏开源库影响数千应用程序|1月10日全球网络安全热点
- LeetCode第874题解析
- Maven项目依赖外部jar进行打包的两种方式