Android入门教程之Vibrator(振动器)

前言:

Vibrator简介:

下面我们就来写个简单的例子,来熟悉下这个Vibrator的用法!

1.获得Vibrator实例:

Vibrator vb = (Vibrator)getSystemService(Service.VIBRATOR_SERVICE);

2.可以使用的相关方法:

1.stract void cancel():关闭或者停止振动器

2.tract boolean hasVibrator():判断硬件是否有振动器

3.id vibrate(long milliseconds):控制手机振动为milliseconds毫秒

4.id vibrate(long[] pattern,int repeat):指定手机以pattern指定的模式振动! 比如:pattern为new int[200,400,600,800],就是让他在200,400,600,800这个时间交替启动与关闭振动器! 而第二个则是重复次数,如果是-1的只振动一次,如果是0的话则一直振动 还有其他两个方法用得不多~ 对了,使用振动器还需要在AndroidManifest.xml中添加下述权限: <uses-permission android:name="android.permission.VIBRATE"/>

3.使用示例:设置频率不同的震动器:

对于Vibrator用的最广泛的莫过于所谓的手机按摩器类的app,在app市场一搜,一堆,笔者随便下了 几个下来瞅瞅,都是大同小异的,这点小玩意竟然有8W多的下载量...好吧,好像也不算多, 不过普遍功能都是切换振动频率来完成,而所谓的按摩效果,是否真的有效就不得而知了, 那么接下来我们就来实现一个简单的按摩器吧! 核心其实就是:vibrate()中的数组的参数,根据自己需求写一个数组就可以了! 下述代码需要在真机上进行测试!

运行效果图:

实现代码:

简单的布局文件,五个按钮:activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/btn_hasVibrator" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="判断是否有振动器" /> <Button android:id="@+id/btn_short" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="短振动" /> <Button android:id="@+id/btn_long" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="长振动" /> <Button android:id="@+id/btn_rhythm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="节奏振动" /> <Button android:id="@+id/btn_cancle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消振动" /> </LinearLayout>

接着是MainActivity.java部分:

public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button btn_hasVibrator; private Button btn_short; private Button btn_long; private Button btn_rhythm; private Button btn_cancle; private Vibrator myVibrator; private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获得系统的Vibrator实例: myVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE); mContext = MainActivity.this; bindViews(); } private void bindViews() { btn_hasVibrator = (Button) findViewById(R.id.btn_hasVibrator); btn_short = (Button) findViewById(R.id.btn_short); btn_long = (Button) findViewById(R.id.btn_long); btn_rhythm = (Button) findViewById(R.id.btn_rhythm); btn_cancle = (Button) findViewById(R.id.btn_cancle); btn_hasVibrator.setOnClickListener(this); btn_short.setOnClickListener(this); btn_long.setOnClickListener(this); btn_rhythm.setOnClickListener(this); btn_cancle.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_hasVibrator: Toast.makeText(mContext, myVibrator.hasVibrator() ? "当前设备有振动器" : "当前设备无振动器", Toast.LENGTH_SHORT).show(); break; case R.id.btn_short: myVibrator.cancel(); myVibrator.vibrate(new long[]{100, 200, 100, 200}, 0); Toast.makeText(mContext, "短振动", Toast.LENGTH_SHORT).show(); break; case R.id.btn_long: myVibrator.cancel(); myVibrator.vibrate(new long[]{100, 100, 100, 1000}, 0); Toast.makeText(mContext, "长振动", Toast.LENGTH_SHORT).show(); break; case R.id.btn_rhythm: myVibrator.cancel(); myVibrator.vibrate(new long[]{500, 100, 500, 100, 500, 100}, 0); Toast.makeText(mContext, "节奏振动", Toast.LENGTH_SHORT).show(); break; case R.id.btn_cancle: myVibrator.cancel(); Toast.makeText(mContext, "取消振动", Toast.LENGTH_SHORT).show(); } } }

对了,别漏了振动器权限哦!

<uses-permission android:name="android.permission.VIBRATE"/>

小结:

好了,本文我们学习了Vibrator(振动器)的基本使用,代码非常简单,还不赶紧加入到 你的APP中,让你的应用HI起来~

时间: 2024-08-02 21:02:45

Android入门教程之Vibrator(振动器)的相关文章

Android入门教程之Vibrator(振动器)_Android

前言: Vibrator简介:  下面我们就来写个简单的例子,来熟悉下这个Vibrator的用法! 1.获得Vibrator实例: Vibrator vb = (Vibrator)getSystemService(Service.VIBRATOR_SERVICE); 2.可以使用的相关方法: 1.stract void cancel():关闭或者停止振动器 2.tract boolean hasVibrator():判断硬件是否有振动器 3.id vibrate(long millisecond

Android入门教程之ListView的应用示例_Android

本文实例讲述了Android ListView的简单应用.分享给大家供大家参考,具体如下: 我们今天要讲的内容是Android中ListView中的实现.一共分为四个步骤,我将一一讲解: Step one:创建一个新的Android工程,命名为ListViewDemo. Step two:找到ListViewDemo.Java,把我们习惯的继承Activity,改成ListActivity,如下: public class ListViewDemo extends ListActivity St

Android入门教程之Picasso框架_Android

一.简介: Picasso是Square公司开源的一个Android图形缓存库.可以实现图片下载和缓存功能. 二.Picasso的特性 Picasso是一个Android图片加载缓存框架,它具有如下特性:       1.支持任务优先级,会优先加载"优先级"较高的图片.       2.带有统计监控功能,可以统计缓存命中率,实时监控已使用的内存等等.       3.能够根据当前网络状态自动调整并发线程数.       4.支持图片的延迟加载.       5.本身不具有本地缓存,而是

Android入门教程之ListView的应用示例

本文实例讲述了Android ListView的简单应用.分享给大家供大家参考,具体如下: 我们今天要讲的内容是Android中ListView中的实现.一共分为四个步骤,我将一一讲解: Step one:创建一个新的Android工程,命名为ListViewDemo. Step two:找到ListViewDemo.Java,把我们习惯的继承Activity,改成ListActivity,如下: public class ListViewDemo extends ListActivity St

Android入门教程之Picasso框架

一.简介: Picasso是Square公司开源的一个Android图形缓存库.可以实现图片下载和缓存功能. 二.Picasso的特性 Picasso是一个Android图片加载缓存框架,它具有如下特性: 1.支持任务优先级,会优先加载"优先级"较高的图片. 2.带有统计监控功能,可以统计缓存命中率,实时监控已使用的内存等等. 3.能够根据当前网络状态自动调整并发线程数. 4.支持图片的延迟加载. 5.本身不具有本地缓存,而是使用的OkHttp实现. Picasso除了使用上比较简单.

PHP入门教程之PHP变量与常量学习

上个月我专门介绍了PHP入门教程中关于PHP基本语法的入门学习,主要介绍了常用的几种PHP标记符,PHP语句的构成,PHP的注释等,今天的PHP入门教程我们主要学习PHP基本语法中PHP变量和常量的基础知识. 针对PHP变量入门学习,本篇入门教程分以下几部分介绍:PHP变量如何标识.PHP变量如何声明.如何给PHP变量赋值.PHP变量的类型介绍.常用PHP变量函数介绍. 针对PHP常量入门学习,主要介绍PHP常量的定义和使用方式. 一.PHP变量如何标识 所谓标识符,其实也就是PHP变量名,主要

Python入门教程之if语句的用法

  这篇文章主要介绍了Python入门教程之if语句的用法,是Python入门的基础知识,需要的朋友可以参考下 Python中的if语句是类似的其它语言的. if语句包含使用该数据进行比较,并根据比较的结果做出了决定的逻辑表达式. 语法: if语句在Python编程语言的语法是: ? 1 2 if expression: statement(s) 如果布尔表达式的计算结果为true,那么if语句块将被执行.如果if语句布尔表达式计算为false,那么第一组代码将被执行. Python编程语言的假

php入门教程之Zend Studio设置与开发实例_php技巧

本文实例讲述了php入门教程之Zend Studio设置与开发方法.分享给大家供大家参考,具体如下: 新建文档的模板设置 新建文档的模板设置 Demo1.php: <?php echo "阅谁问君诵,水落清香浮." ?> orderform.php: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD

Phalcon入门教程之Volt模板引擎

原文发表于:Phalcon入门教程之Volt模板引擎 volt 是Phalcon中集成的模板引擎,我们也可以更换为其他模板引擎或同时使用多个模板引擎.本文只介绍 Phalcon 自带的 volt 模板引擎. 启用Volt 和其他模板引擎一样,我们需要将 volt 模板注册到 views 组件中,并设置模板文件通用后缀名,或者直接使用标准化的后缀名 .phtml 才能正常使用: //文件路径:Marser\App\Frontend\FrontendModule.php $di->setShared