Android实现调用震动的方法
作者:leeon 发布时间:2021-10-03 19:33:24
本文实例讲述了Android实现调用震动的方法。分享给大家供大家参考,具体如下:
调用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
* 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程序设计有所帮助。


猜你喜欢
- 如下所示:public static String reThreeStr(String ss){boolean result= ss.mat
- 从这章开始,会介绍几个常用的函数式接口工具,首先先来看下这个大家族:首先从Function接口开始介绍一. 概述该接口顾名思义,函数的意思,
- 这篇文章主要介绍了Java内存缓存工具Guava LoadingCache使用解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有
- 提到Java发送HTTP请求,大家首先想到的是用apache的HttpClient,或者squareup的OkHttp。而在Java11之前
- 本文实例讲述了C#同步网络时间的方法。分享给大家供大家参考。具体分析如下:客户的机器的系统时间经常出错,导致给他们做的软件无法正常使用,所以
- 目录获取程序下面的文件获取临时目录下的文件获取程序下面的文件首先我们创建了实例解决方案:其中调用链是:Main.Shell->FooA
- Android Studio安装后发现所有的中文,不管是界面上的还是输出的log中的中文都变成小框框 可以肯定是字体的问题 解决
- hibernate一级缓存和二级缓存的区别缓存是介于应用程序和物理数据源之间,其作用是为了降低应用程序对物理数据源访问的频次,从而提高了应用
- 前言一般情况下,多数移动开发者使用的是数据线连接电脑,进行各种移动设备的调试,更有胜者,非常迷恋模拟器,模拟器它好不好,答案是好,因为直接运
- java简单模拟微信抢红包功能,本例发100元红包,有10个人抢,为了尽可能的公平,每个人的红包金额都要随机(保证结果的不确定性,本例抢红包
- 1、什么是 IOC?IOC-Inversion of Control,即控制反转。它不是什么技术,而是一种设计思想。传统的创建对象的方法是直
- 在默认情况下,对象的Equals(object o)方法(基类Object提供),是比较两个对象变量是否引用同一对象。我们要必须我自己的对象
- 目录1.前言2.不同进制的特点3.进制之间的转换3.1 二进制转十进制:3.2 十进制转二进制:3.3 二进制转八进制:3.4 十六进制转二
- 什么是RecyclerViewRecyclerView 是Google推出的最新的 替代ListView、GridView的组件
- Java基础之理解Annotation一、概念 Annontation是Java5开始引入的新特征。中文名称一般叫注解。它提供了一
- 本文实例为大家分享了用JavaMail发送HTML模板邮件的具体代码,供大家参考,具体内容如下依赖<dependency>&nb
- Android 活动条ActionBar的详解图一 图二 图三 图四 图五 ActionBar其提供的功能总结图一使用ActionB
- 本文实例为大家分享了Android实现五子棋游戏的具体代码,供大家参考,具体内容如下直接上效果图原理从棋盘到棋子,到开始下棋的各类点击事件,
- 说明SpringBoot版本:2.1.4.RELEASEjava版本:1.8文中所说JPA皆指spring-boot-starter-dat
- 摘要 想必大家对小榕时光等扫描器都非常熟悉了,有没有自己写一个的冲动。最近微软推实施了.NET战略方案,C#是主推语言,你们是否