本文实例讲述了Android实现调用震动的方法。分享给大家供大家参考,具体如下:

调用Android系统的震动,只需要一个类 那就是Vibrator ,这个类在hard包中,一看系统级的服务,又要通过manifest.xml文件设置权限了

package="uni.vibrator"

android:versionCode="1"

android:versionName="1.0">

android:label="@string/app_name">

下面还是一起学习一下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

* 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程序设计有所帮助。

android 怎么调用震动,Android实现调用震动的方法相关推荐

  1. android 广播震动,Android实现调用震动的方法

    本文实例讲述了Android实现调用震动的方法.分享给大家供大家参考,具体如下: 调用Android系统的震动,只需要一个类 那就是Vibrator ,这个类在hard包中,一看系统级的服务,又要通过 ...

  2. android 点击震动,Android应用中实现点击按钮震动

    项目中需要实现点击应用中的按钮,有震动效果.震动效果可以在设置中进行关闭. 具体实现如下. 编写震动帮助类:VibrateHelp.java import android.content.Contex ...

  3. android 点击震动,Android 实现为点击事件添加震动效果

    Android 点击Button 实现震动效果教程 Overview 在Android 的点击效果中,遇到震动效果的还是很多的. 接下来就让我们看一下如何实现震动效果. 所需要的权限 如果我们在开发中 ...

  4. 调用实现天气预报功能android,Android编程实现获取新浪天气预报数据的方法

    本文实例讲述了Android编程实现获取新浪天气预报数据的方法.分享给大家供大家参考,具体如下: 新浪天气预报地址: http://php.weather.sina.com.cn/xml.php?ci ...

  5. Unity调用安卓Android的Toast

    需求:在游戏中弹窗消息,调起安卓的Toast 项目中需要做Unity和安卓交互时,经常需要通过安卓Toast来做简单的输出,以便于测试. 方法一:Unity中,C#主导 // Unity调用安卓的土司 ...

  6. Android之ksoap2-android详解与调用天气预报Webservice完整实例

    Google为Android平台开发Web Service客户端提供了ksoap2-android项目,在这个网址下载开发包http://code.google.com/p/ksoap2-androi ...

  7. Android WebView 和 javaScript的互相调用(三)

    原文出处:http://motalks.cn/2016/08/27/Android-WebView-JavaScript-2/ WebView相关阅读 Android WebView 和 javaSc ...

  8. Android WebView 和 javaScript的互相调用(二)

    原文出处:http://blog.csdn.net/wangtingshuai/article/details/8635787 WebView相关阅读 Android WebView 和 javaSc ...

  9. Android WebView 和 javaScript的互相调用(一)

    原文出处:http://blog.csdn.net/wangtingshuai/article/details/8631835 WebView相关阅读 Android WebView 和 javaSc ...

最新文章

  1. 令人机双双崩溃的VS2008 SP1!WPF用户请勿更新!
  2. 为啥channel能做到线程安全
  3. 12C OCP 1ZO-071 题库(8月以前)
  4. 传统配置形式存在的问题
  5. 华为光模块,华为光纤模块,华为单模光模块,华为多模光模块,华为千兆光模块
  6. ZOJ 1586 QS Network
  7. Python异常处理和进程线程
  8. flask-配置的设置-三种配置的实现方法
  9. android8.0应用图标适配调整_怎样更换macOS Big Sur 应用图标
  10. 查询英语单词 - 有道官方(一)
  11. 【electron】nsis重编译,自定义nsis校验弹窗警告的文案
  12. 5python 体脂率计算(优化版)
  13. 绘制折线图分析股票涨跌
  14. 制作u盘winpe启动盘_u盘启动盘制作教程
  15. java算法——通过身份证号获取出生的年月日
  16. 用键盘输入一位整数,当输入1~7时,显示对应的英文星期名称的缩写。
  17. 解决com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
  18. 企业端点网络安全行业调研报告 - 市场现状分析与发展前景预测
  19. JN5169 NXP Zigbee 3.0开发环境搭建
  20. js实现实时问候,不同时间显示不同问候语

热门文章

  1. 搭建一个点歌QQ机器人,另外还能看美女
  2. 通过对比3PL和4PL,来了解什么是4PL
  3. LeetCode 面试题 峰与谷
  4. EMC规划BRS产品线 推动私有云建设
  5. 周鸿袆: 教您打造十页完美商业计划书
  6. 非诚勿扰 11位骗子全是托 愚乐节目 愚弄观众 请勿相信节目内容
  7. ffmpeg转换avi、mp4等视频格式为yuv格式
  8. 取供应商类型所在的快码
  9. AWS-EC2多弹性ip配置
  10. 使用线程池优化多线程编程