Android实现调用震动的方法

 更新时间:2015年11月11日 14:56:14   作者:leeon  
这篇文章主要介绍了Android实现调用震动的方法,实例分析了Android中Vibrator类的调用与使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了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热修复技术原理之so库热修复技术

    深入理解Android热修复技术原理之so库热修复技术

    通常情况下,大多数人希望android下热补丁方案能够做到补丁的全方位修复,包括类修复/资源修复/so库的修复。 这里主要介绍热补丁之so库修复思路
    2021-06-06
  • 详解flutter engine 那些没被释放的东西

    详解flutter engine 那些没被释放的东西

    这篇文章主要介绍了详解flutter engine 那些没被释放的东西,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • Android VNDK使用及原理深入探究

    Android VNDK使用及原理深入探究

    这篇文章主要为大家介绍了Android VNDK使用及原理深入探究,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-01-01
  • 详解android系统的定制

    详解android系统的定制

    这篇内容给大家分享了关于android系统的定制的一些步骤和基本知识点,有兴趣的朋友参考学习下。
    2018-06-06
  • java创建线程的4种方式集合(最新整理)

    java创建线程的4种方式集合(最新整理)

    在Java中,创建线程有多种方式,每种方式都有其适用场景和优缺点,下面给大家分享java创建线程的4种方式,感兴趣的朋友一起看看吧
    2018-05-05
  • Android判断wifi是5G还是2.4G

    Android判断wifi是5G还是2.4G

    这篇文章给大家分享android区分wifi是5G还是2.4G的方法,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下
    2016-12-12
  • Mac Android Studio安装图文教程

    Mac Android Studio安装图文教程

    本文主要介绍了Mac Android Studio安装教程,文中通过图文代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • Android编程实现震动与振铃的方法详解

    Android编程实现震动与振铃的方法详解

    这篇文章主要介绍了Android编程实现震动与振铃的方法,结合实例形式分析了Android实现震动与振铃的Vibrator类及MediaPlayer类相关使用技巧,需要的朋友可以参考下
    2018-03-03
  • Android 捕获错误日志的方法

    Android 捕获错误日志的方法

    这篇文章主要介绍了Android 捕获错误日志的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • Android Studio使用Profiler来完成内存泄漏的定位

    Android Studio使用Profiler来完成内存泄漏的定位

    这篇文章主要介绍了Android Studio使用Profiler来完成内存泄漏的定位,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下
    2021-03-03

最新评论