广告
返回顶部
首页 > 资讯 > 移动开发 >Android实现调用震动的方法
  • 166
分享到

Android实现调用震动的方法

调用方法Android 2022-06-06 09:06:43 166人浏览 安东尼
摘要

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

本文实例讲述了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,用来取消震动

看一段演示的代码:



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;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    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滑动解震动效果的开启和取消android获取情景模式和铃声 实现震动、铃声提醒android 触屏的震动响应接口调用方法Android实现手机震动效果Android震动与提示音实现代码android闹铃简单实现Android设置铃声实现代码Android闹铃服务AlarmManager用法深入分析Android编程实现震动与振铃的方法详解


--结束END--

本文标题: Android实现调用震动的方法

本文链接: https://www.lsjlt.com/news/26370.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作