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. Response.Redirect 打开这两种方法的一种新形式

    在一般情况下.Response.Redirect 该方法是在server年底转向,因此,除非 Response.Write("<script>window.location='h ...

  2. Org-mode五分钟教程ZZZ - Kaka Abel的日志 - 网易博客

    Org-mode五分钟教程ZZZ - Kaka Abel的日志 - 网易博客 Org-mode五分钟教程ZZZ  

  3. 对struts2的OGNL的理解

    OGNL:Object-Graph Navigation Language.对象图形化导航语言 OGNL是集成进struts2框架中比較强大的技术有助于传输数据和类型转换,OGNL由表达式语言和类型装 ...

  4. [Cocos2d-x]代码段记录

    一些零碎的代码,便于以后查找 1.添加动画 //添加动画帧 CCAnimation* animation = CCAnimation::create(); ; i< ;i++) { ] = {} ...

  5. eclipse package,source folder,folder差别及相互转换

    在eclipse下, package, source folder, folder都是目录.   它们的差别例如以下:   package:当你在建立一个package时,它自己主动建立到source ...

  6. 【智能家居篇】wifi网络接入原理(上)——扫描Scanning

    转载请注明出处:http://blog.csdn.net/Righthek 谢谢! 对于低头党来说,在使用WIFI功能时,常常性的操作是打开手机上的WIFI设备,搜索到心目中的热点,输入passwor ...

  7. 图片本地预览 flash html5

    dataURI 一种能够在页面嵌入外部资源的URI方案.能够降低图片或者样式表的http请求数量,提高效率. ie8把dataURI 的属性值限制在32k以内. 图片本地预览: 由于安全原因,通过fi ...

  8. hdu 1240 Asteroids!(BFS)

    题目链接:点击链接 简单BFS,和二维的做法相同(需注意坐标) 题目大意:三维的空间里,给出起点和终点,“O”表示能走,“X”表示不能走,计算最少的步数 #include <iostream&g ...

  9. 访何红辉:谈谈Android源码中的设计模式

    最近Android 6.0版本的源代码开放下载,刚好分析Android源码的技术书籍<Android源码设计模式解析与实战>上市,我们邀请到它的作者何红辉,来谈谈Android源码中的设计 ...

  10. 深入浅出Windows BATCH

    1.什么是Windows BATCH BATCH也就是批处理文件,有时简称为BAT,是Windows平台上的一种可运行脚本,与*nix(Linux和Unix)上的Shell脚本和其它的脚本(Perl, ...