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

标签:Android,震动
0
投稿

猜你喜欢

  • 详解Springboot自定义异常处理

    2021-11-04 17:15:12
  • java微信开发中的地图定位功能

    2023-01-27 17:37:52
  • Java中IO流解析及代码实例详解

    2022-03-08 22:32:16
  • Unity调用手机摄像机识别二维码

    2023-05-18 23:56:36
  • AsyncTask官方文档教程整理

    2023-07-31 20:25:08
  • Android 检测键盘显示或隐藏键盘的实现代码

    2022-09-08 09:14:05
  • SpringMVC底层执行流程及原理解析

    2022-11-19 03:34:56
  • Java 抽象类与接口的对比

    2022-05-31 08:28:30
  • Mybatis在sqlite中无法读写byte[]类问题的解决办法

    2021-08-06 21:06:07
  • Android N多窗口支持

    2022-08-08 19:27:37
  • Unity3D实现人物转向与移动

    2022-10-28 08:42:13
  • Java String类的理解及字符串常量池介绍

    2022-11-14 15:42:22
  • go语言题解LeetCode88合并两个有序数组示例

    2023-10-23 00:55:10
  • C#合并多种格式文件为PDF的方法

    2023-12-11 17:46:58
  • Mybatis-Plus分页的使用与注意事项

    2022-08-14 22:00:56
  • 基于Java中的数值和集合详解

    2023-11-25 08:12:13
  • 关于C# 5.0 CallerMemberName CallerFilePath CallerLineNumber 在.NET4中的使用介绍方法

    2022-03-27 22:05:56
  • 关于Spring Data Jpa 自定义方法实现问题

    2023-11-28 10:08:32
  • Java数组队列概念与用法实例分析

    2023-11-18 04:18:31
  • Spring Aop 源码增强获取分享

    2023-06-22 21:59:22
  • asp之家 软件编程 m.aspxhome.com