Android该系统提供的服务--Vibrator(振子)

——转载请注明出处:coder-pig

Vibrator简单介绍与相关方法:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY29kZXJfcGln/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

简单demo——设置频率不同的振动器

对于Vibrator用的最广泛的莫过于所谓的手机按摩器类的app,在app市场一搜,一堆,笔者随便下了几个下来瞅瞅

,都是大同小异的,这点小玩意居然有8W多的下载量...好吧,好像也不算多,只是普遍功能都是切换振动频率来完毕

所谓的按摩效果,是否真的有效就不得而知了,那么接下来

我们就来实现一个简单的按摩器吧!

核心事实上就是vibrate()中的数组的參数,依据自己需求写一个数组就能够了!

由于模拟器不会振动的,所以须要在手机上执行才会有效果哦!

效果图:



代码也非常easy,布局的话就四个简单的button而已

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.jay.example.vibratordemo.MainActivity" > <Button
android:id="@+id/btnshort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="短振动" /> <Button
android:id="@+id/btnlong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="长振动" /> <Button
android:id="@+id/btnrhythm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="节奏振动" /> <Button
android:id="@+id/btncancle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消振动" />
</LinearLayout>

接着就是MainActivity的编写了,这里和上一节的写法是一样的,让Activity类实现OnClickListener接口

重写onClick方法,依据不同的view的id就能够推断是哪个button点击了,这样的写法的优点是对于组件较多的

情况这样写能够简化代码!

MainActivity.java:

package com.jay.example.vibratordemo;

import android.app.Activity;
import android.app.Service;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener{ private Button btnshort;
private Button btnlong;
private Button btnrhythm;
private Button btncancel;
private Vibrator myVibrator; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); btnshort = (Button) findViewById(R.id.btnshort);
btnlong = (Button) findViewById(R.id.btnlong);
btnrhythm = (Button) findViewById(R.id.btnrhythm);
btncancel = (Button) findViewById(R.id.btncancle); btnshort.setOnClickListener(this);
btnlong.setOnClickListener(this);
btnrhythm.setOnClickListener(this);
btncancel.setOnClickListener(this); //获得系统的Vibrator实例:
myVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE); } @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnshort:
myVibrator.cancel();
myVibrator.vibrate(new long[]{100,200,100,200}, 0);
Toast.makeText(getApplicationContext(), "短振动", Toast.LENGTH_SHORT).show();
break;
case R.id.btnlong:
myVibrator.cancel();
myVibrator.vibrate(new long[]{100,100,100,1000}, 0);
Toast.makeText(getApplicationContext(), "长振动", Toast.LENGTH_SHORT).show();
break;
case R.id.btnrhythm:
myVibrator.cancel();
myVibrator.vibrate(new long[]{500,100,500,100,500,100}, 0);
Toast.makeText(getApplicationContext(), "节奏振动", Toast.LENGTH_SHORT).show();
break;
case R.id.btncancle:
myVibrator.cancel();
Toast.makeText(getApplicationContext(), "取消振动", Toast.LENGTH_SHORT).show();
} }
}

最后别忘了在AndroidManifest.xml文件里加入权限哦!

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

好了,基本使用方法事实上也是非常easy的,这里就不多说了,另外上面也说了,虚拟机是没有震动效果的,所以

须要把应用公布到手机上才干检測效果了!參考代码下载:vibratorDemo.rar

为了方便各位,直接把apk导出来吧,直接下载安装到手机就能够測试效果了,当然仅仅是个小demo,不会

推送广告,乱发短信什么的=-=!安装时能够查看所须要的权限!apk下载:vibratorDemo.apk

Android该系统提供的服务--Vibrator(振子)的更多相关文章

  1. Android系统提供的开发常用的包名及作用

    android.app :提供高层的程序模型.提供基本的运行环境 android.content :包含各种的对设备上的数据进行访问和发布的类 android.database :通过内容提供者浏览和 ...

  2. Android 震动马达系统

    Android之 看"马达"如何贯通Android系统 (从硬件设计 --> 驱动 --> HAL --> JNI --> Framework --> ...

  3. Android_(服务)Vibrator振动器

    Vibrator振动器是Android给我们提供的用于机身震动的一个服务,例如当收到推送消息的时候我们可以设置震动提醒,也可以运用到游戏当中增强玩家互动性 运行截图: 程序结构 <?xml ve ...

  4. android Gui系统之WMS(1)----window flags & view flags

    SurfaceFlinger 前面说的,就是一个surface的合成.SurfaceFlinger就是一个默默的记录着,它不会对surface的内容有什么改动. WMS(WindowsManagerS ...

  5. 图解Android - Android GUI 系统 (1) - 概论

    Android的GUI系统是Android最重要也最复杂的系统之一.它包括以下部分: 窗口和图形系统 - Window and View Manager System. 显示合成系统 - Surfac ...

  6. 图解Android - Android GUI 系统 (2) - 窗口管理 (View, Canvas, Window Manager)

    Android 的窗口管理系统 (View, Canvas, WindowManager) 在图解Android - Zygote 和 System Server 启动分析一 文里,我们已经知道And ...

  7. 图解Android - Android GUI 系统 (5) - Android的Event Input System

    Android的用户输入处理 Android的用户输入系统获取用户按键(或模拟按键)输入,分发给特定的模块(Framework或应用程序)进行处理,它涉及到以下一些模块: Input Reader: ...

  8. Android入门:绑定本地服务

    一.绑定服务介绍   前面文章中讲过一般的通过startService开启的服务,当访问者关闭时,服务仍然存在: 但是如果存在这样一种情况:访问者需要与服务进行通信,则我们需要将访问者与服务进行绑定: ...

  9. Android的系统架构

    转自Android的系统架构 从上图中可以看出,Android系统架构为四层结构,从上层到下层分别是应用程序层.应用程序框架层.系统运行库层以及Linux内核层,分别介绍如下:     1)应用程序层 ...

随机推荐

  1. IOT表优缺点

    <pre name="code" class="html">IOT表是将所有东西都塞到叶块中,表就是索引,可以避免回表 首先,对于IOT而言,只有索 ...

  2. 怎样在Android实现桌面清理内存简单Widget小控件

    怎样在Android实现桌面清理内存简单Widget小控件 我们常常会看到类似于360.金山手机卫士一类的软件会带一个widget小控件,显示在桌面上,上面会显示现有内存大小,然后会带一个按键功能来一 ...

  3. MingW环境下的windows编程

    一般在进行windows编程时都使用vc++精简版,其插入菜单,图片等资源等更简单,且vc中对中文有更好的支持,win7下安装的Mingw中文并不能很好地显示,有光标显示的位置和光标实际位置不符的问题 ...

  4. 基于Predictive Parsing的ABNF语法分析器(十三)——rulelist、rule、rulename、define-as和elements

    我们来看看rulelist,它是整个ABNF文法的入口,就是说一个ABNF文法就是一个规则列表rulelist.一个rulelist由若干个rule规则组成,每个rule由规则名rulename.定义 ...

  5. 用gradle管理android项目出现的问题以及解决方法

    1.项目结构 最好是全部在root 项目配置 一个settings.gradle 一个build.gradle 2.多项目依赖 http://www.gradle.org/docs/current/u ...

  6. TD-SCDMA风雨20年:中国3G标准的由来以及国家通信战略

    .国际电信标准是咋回事? 当年作为通信专业的学生,我曾长期困惑一个问题,为什么同一项通信技术总会有美国和欧洲两种国际标准?比如电话语音的数字化就有欧洲A律和美国u(谬)律两种. 学习后发现,两种标准的 ...

  7. zoj2760(最大流)

    传送门:How Many Shortest Path 题意:给出n个点,和n*n的矩阵表示有向图.a[i][j]为-1表示i到j没有路径:不为-1则表示i到j的路径长度.给出一个vs和vt,要求vs到 ...

  8. Application to find the maximum temperature in the weather dataset

    import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop. ...

  9. WebService 通过POST方式访问时候,因 URL 意外地以“/方法名”结束,请求格式无法识别 解决办法

    因URL意外地以“/方法名”结束,请求格式无法识别. 执行当前Web请求期间生成了未处理的异常.可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息. 解决方法:在webservice的we ...

  10. Java 使用AES/CBC/PKCS7Padding 加解密字符串

    介于java 不支持PKCS7Padding,只支持PKCS5Padding 但是PKCS7Padding 和 PKCS5Padding 没有什么区别要实现在java端用PKCS7Padding填充, ...