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)应用程序层 ...
随机推荐
- oracle检查点队列与增量检查点【转载】
oracle检查点队列与增量检查点 今天是2013-09-04,这几天一直心里安顿不下来,今天还好了,可以自己安静的学习一下oracle,在此记录一下学习笔记.这篇文章我不知道在那转载的,一直都留在我 ...
- Delphi数据类型转换(有几个字符串函数没见过,比如StringToWideChar和WideCharToString)
DateTimeToFileDate 函数 将DELPHI的日期格式转换为DOS的日期格式 DateTimeT ...
- [SVN]两个分支合并
Date:2014-1-1 Summary: 记录一下自己使用SVN时候的操作步骤,先吃鱼,再学钓鱼 Contents: 环境:从同事的branch迁出一份代码,作为自己的分支进行开发,同时同事也在自 ...
- Java程序员们最常犯的10个错误(转)
1.将数组转化为列表 将数组转化为一个列表时,程序员们经常这样做: 1 List<String> list = Arrays.asList(arr); Arrays.asList(&quo ...
- ios应用接入微信开放平台
前几天试了一下服务端接入微信公众平台,昨天又看了一下APP接入开放平台 开放平台和公众平台的差别 公众平台针对的是公众账号,除了提供管理后台之外.也开放了若干接口,让微信server和开发人员自己的应 ...
- Pyocr 0.2 发布,Python 的 OCR 库 - 开源中国社区
Pyocr 0.2 发布,Python 的 OCR 库 - 开源中国社区 Pyocr 0.2 发布,Python 的 OCR 库
- 微信支付.net官方坑太多,我们来精简
原文:微信支付.net官方坑太多,我们来精简 微信支付官方坑太多,我们来精简 我把官方的代码,打包成了 an.wxapi.dll. 里面主要替换了下注释.呵呵.然后修改了几个地方. 修改一.Confi ...
- 【C语言疯狂讲义】(八)C语言一维数组
1.数组的基本概念: 同样类型 若干个 有序 由若干个同样类型的数据组成的有序的集合 有序:存储地址连续 下标连续 数组名:用来存放数组首地址的变量 数组元素:构成数组的每个数据 数组的 ...
- hdu1243(最长公共子序列变形)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1243 分析:dp[i][j]表示前i个子弹去炸前j个恐怖分子得到的最大分.其实就是最长公共子序列加每个 ...
- 蓝桥杯 【dp?】.cpp
题意: 给出一个2*n的方格,当刷完某一个方格的漆后可以且只可以走到相邻的任何一格,即上 下 左 右 左上 左下 右上 右下.可以从任意一个格子开始刷墙,问有多少种刷法,因为随着n的增大方案数会变多, ...