Android该系统提供的服务--Vibrator(振子)
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(振子)的更多相关文章
- Android系统提供的开发常用的包名及作用
android.app :提供高层的程序模型.提供基本的运行环境 android.content :包含各种的对设备上的数据进行访问和发布的类 android.database :通过内容提供者浏览和 ...
- Android 震动马达系统
Android之 看"马达"如何贯通Android系统 (从硬件设计 --> 驱动 --> HAL --> JNI --> Framework --> ...
- Android_(服务)Vibrator振动器
Vibrator振动器是Android给我们提供的用于机身震动的一个服务,例如当收到推送消息的时候我们可以设置震动提醒,也可以运用到游戏当中增强玩家互动性 运行截图: 程序结构 <?xml ve ...
- android Gui系统之WMS(1)----window flags & view flags
SurfaceFlinger 前面说的,就是一个surface的合成.SurfaceFlinger就是一个默默的记录着,它不会对surface的内容有什么改动. WMS(WindowsManagerS ...
- 图解Android - Android GUI 系统 (1) - 概论
Android的GUI系统是Android最重要也最复杂的系统之一.它包括以下部分: 窗口和图形系统 - Window and View Manager System. 显示合成系统 - Surfac ...
- 图解Android - Android GUI 系统 (2) - 窗口管理 (View, Canvas, Window Manager)
Android 的窗口管理系统 (View, Canvas, WindowManager) 在图解Android - Zygote 和 System Server 启动分析一 文里,我们已经知道And ...
- 图解Android - Android GUI 系统 (5) - Android的Event Input System
Android的用户输入处理 Android的用户输入系统获取用户按键(或模拟按键)输入,分发给特定的模块(Framework或应用程序)进行处理,它涉及到以下一些模块: Input Reader: ...
- Android入门:绑定本地服务
一.绑定服务介绍 前面文章中讲过一般的通过startService开启的服务,当访问者关闭时,服务仍然存在: 但是如果存在这样一种情况:访问者需要与服务进行通信,则我们需要将访问者与服务进行绑定: ...
- Android的系统架构
转自Android的系统架构 从上图中可以看出,Android系统架构为四层结构,从上层到下层分别是应用程序层.应用程序框架层.系统运行库层以及Linux内核层,分别介绍如下: 1)应用程序层 ...
随机推荐
- Swift - 滚动视图(UIScrollView)的用法
1,当图片尺寸超过屏幕时,使用UIScrollView可以实现滚动条视图,即手指触摸滚动屏幕方便浏览整个页面. 1 2 3 4 5 6 var scrollView=UIScrollView() sc ...
- Delphi默认窗体随想
Delphi中新建一个Form或者Frame时,它的字体都是西文习惯,这样就有可能造成在其他机器上由于字体的原因,窗体十分不美观.怎样才能为Delphi设置一个默认窗体,让它的字体Font符合中国习惯 ...
- 利用WinDbg找出程序崩溃的代码行号
之前碰到论坛里有几个好友,说程序不时的崩溃,什么xxoo不能read的! 如果光要是这个内存地址,估计你会疯掉~~ 所以分享一下基本的调试技巧,需要准备的工具有WinDbg + VC6.0, 下面是自 ...
- Android显示GIF图片
今天我们研究一下怎样在Android手机上显示GIF动态图片 首先须要在src文件夹下新建一个自己定义的View.代码例如以下: </pre><pre name="code ...
- hdu2066一个人的旅行
枚举全部相邻城市,作为起点,多次spfa,然后每次在想去的城市中找出spfa后的距离起点最短的花费时间 #include <iostream> #include <cstring&g ...
- C#反射Assembly 具体说明
1.对C#反射机制的理解 2.概念理解后,必须找到方法去完毕,给出管理的主要语法 3.终于给出有用的样例,反射出来dll中的方法 反射是一个程序集发现及执行的过程,通过反射能够得到*.exe或*.dl ...
- MySQL触发器 trigger之for each row
for each row 每行受影响,触发器都运行.叫行级触发器. oracle 触发器中分行级触发器和语句级触发器,可不写for each row,不管影响多少行都仅仅运行一次. mysql不支持语 ...
- HDU 4611 Balls Rearrangement (数学-思维逻辑题)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4611 题意:给你一个N.A.B,要你求 AC代码: #include <iostream> ...
- java常见的输入和输出流案例研究(一个)
字节输入和输出流 1.FileInputStream[文件字节输入流]->读取文件内容 用途:从文件系统中的文件获得输入字节.经常使用于读取图像.声音等原始字节流,读取字符流可考虑使用FileR ...
- java.lang.IllegalAccessError: class javax.activation.SecuritySupport12 cannot access its superclass
最近加入新的项目组,eclipse + tomcat7 + spring +ibatis + restful 遇到了这样的问题, 说是不能访问父类,我一开始以为是版本的原因,但是久经更改,错误依然,实 ...