Vibrator振动器是Android给我们提供的用于机身震动的一个服务,例如当收到推送消息的时候我们可以设置震动提醒,也可以运用到游戏当中增强玩家互动性


运行截图:

程序结构

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> <uses-permission android:name="android.permission.VIBRATE"/> </manifest>

AndroidManifest.xml

package com.example.administrator.myapplication;

import android.app.Service;

import android.content.Context;

import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.view.View;
import android.widget.Button; import android.widget.Toast; 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();
}
}
}

MainActivity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.myapplication.MainActivity"
android:weightSum="1"> <Button
android:id="@+id/btn_hasVibrator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="监测手机是否有振动器" /> <Button
android:id="@+id/btn_short"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="短振动" /> <Button
android:id="@+id/btn_long"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="长振动" /> <Button
android:id="@+id/btn_rhythm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="节奏振动" /> <Button
android:id="@+id/btn_cancle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消振动" /> </LinearLayout>

activity_main.xml

一、获得系统的Vibrator

        myVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
mContext = MainActivity.this;

二、判断并设置频率不同的震动器

 

abstract void cancel():关闭或者停止振动器

abstract boolean hasVibrator():判断硬件是否有振动器

void vibrate(long milliseconds):控制手机振动为milliseconds毫秒
void vibrate(long[] pattern,int repeat):指定手机以pattern指定的模式振动

第一个参数:new int[200,400,600,800],在200,400,600,800这个时间交替启动与关闭振动器
第二个参数:重复次数,如果是-1的只振动一次,如果是0的话则一直振动
 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"/>

Android_(服务)Vibrator振动器的更多相关文章

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

    Android该系统提供的服务--Vibrator(振子) --转载请注明出处:coder-pig Vibrator简单介绍与相关方法: watermark/2/text/aHR0cDovL2Jsb2 ...

  2. Android中振动器(Vibrator)的使用

    系统获取Vibrator也是调用Context的getSystemService方法,接下来就可以调用Vibrator的方法控制手机振动了.Vibrator只有三个方法控制手机振动: 1.vibrat ...

  3. 振动器(Vibrator)

    package com.wwj.serviceandboardcast;   import android.app.Activity; import android.app.Service; impo ...

  4. Android 振动器

    今天介绍一下Android的振动器Vibrator,有三个方法来控制手机振动: 1.void vibrate(long milliseconds):控制手机振动milliseconds毫秒. 2.vo ...

  5. Android菜鸟的成长笔记(24)——Android中的振动器

    在某些时候,程序需要启动系统振动器,比如手机静音时使用振动提示用户:再比如玩游戏时,当系统碰撞.爆炸时使用振动带给用户更逼真的体验等.总之,振动是除视频.声音之外的另一种"多媒体" ...

  6. Android的几种Manager

    电话管理器TelephoneManager 第一个实例是获取网络和SIM卡信息:界面是一个列表,这里省略,Java代码如下: public class MainActivity extends Act ...

  7. Micro:Bit手柄试用之一MagicPad (解决蓝牙与gamePad包共存)

    前言 原创文章,转载引用务必注明链接.由于本人初次接触Micro:Bit,水平有限,如有疏漏,欢迎指正. Micro:Bit真好玩! DFRobot的论坛相关资料算是国内比较丰富的了,个人感觉MB比A ...

  8. 利用传感器(sensor)实现微信摇一摇动画

    所需要的权限: <uses-permission android:name="android.permission.VIBRATE"></uses-permiss ...

  9. 玩转Android之加速度传感器的使用,模仿微信摇一摇

    Android系统带的传感器有很多种,最常见的莫过于微信的摇一摇了,那么今天我们就来看看Anroid中传感器的使用,做一个类似于微信摇一摇的效果. OK ,废话不多说,我们就先来看看效果图吧: 当我摇 ...

随机推荐

  1. Docker 容器简介与部署

    关于Docker容器技术 参考文献:<docker 从入门到精通> Docker容器简介 Docker的构想是要实现 "Build,Ship and Run Any App,An ...

  2. QuickSort(快排)的JAVA实现

    QuickSort的JAVA实现 这是一篇算法课程的复习笔记 用JAVA对快排又实现了一遍. 先实现的是那个easy版的,每次选的排序轴都是数组的最后一个: package com.algorithm ...

  3. 07 MySQL之索引原理

    一.介绍 为什么有索引:使用索引可快速访问数据库表中的特定信息.索引是对数据库表中一列或多列的值进行排序的一种结构. 作用: 1.        快速查询数据 2.        保证数据的唯一性 3 ...

  4. java中怎么跳出两层for循环

    使用标号(使用标号跳出两层或者多层for循环): outterLoop: for (int i = 0; i < 9; i++){             for (int j = 0; j & ...

  5. thinkphp漏洞集合

    整合了一个集合,方便查询 thinkphp 5.0.22 1.http://192.168.1.1/thinkphp/public/?s=.|think\config/get&name=dat ...

  6. 韦东山嵌入式Linux学习笔记07--Nandflash

    常用的flash有两种, Norflash和Nandflash, 前几年市场上的产品比较常见的方案时Norflash和Nandflash搭配使用, 因为norflash比较昂贵,相同的容量norfla ...

  7. 运维都该会的Socket知识!

    本篇博客转自赵班长 所有运维都会的Socket知识!!! 原创: 赵班长 新运维社区 什么是Socket? 大家都用电脑上网,当我们访问运维社区https://www.unixhot.com的时候,我 ...

  8. A RECURRENT NEURAL NETWORK WITHOUT CHAOS

    本篇文章的介绍了一个非常简单的门限RNN(gated recurrent neural network), 这里有两扇门horizontal/forget gate和vertical/input ga ...

  9. target_link_libraries每次能连接1个???

    target_link_libraries(usb-1.0)target_link_libraries(cyusb)

  10. CentOS 7 中英文桌面安装步骤详细图解

    https://www.cnblogs.com/haoliyou/p/7694868.html