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"/>

最新文章

  1. 11——在operator=中处理自我赋值
  2. github 多帐户使用
  3. Unity3D中关于场景销毁时事件调用顺序的一点记录
  4. 高亮选中MEMO某一行
  5. SQL系统函数的使用(实验五)
  6. spring拦截器的简单实现Interceptor
  7. Springboot+ mybatis+ mysql配置@Slf4j
  8. android各种笔记
  9. Maven初窥门径
  10. windows下使用kafka的常用命令
  11. RSA 算法
  12. asp.net之发送邮件1
  13. 扩展music-list.vue让列表前三名显示&#127942;奖杯
  14. OC 里面 webView与js
  15. BZOJ3157/BZOJ3516 国王奇遇记(矩阵快速幂/数学)
  16. java之压缩流(ZipOutputStream)
  17. Redis协议规范(译文)
  18. 接口测试工具postman(六)添加变量(参数化)
  19. BZOJ4813 CQOI2017小Q的棋盘(树形dp)
  20. 从头开始学eShopOnContainers——开发环境要求

热门文章

  1. python computer info look
  2. python基础_面向对象进阶
  3. python多任务——协程的使用
  4. 安装gitlab ce
  5. C# 如何正确删除控件已添加的事件
  6. NTL 库函数
  7. Session的生命周期和工作原理
  8. 在mysql 上如何在不影响生产的情况下删除一个大表
  9. 使用QEMU模拟树莓派
  10. select,poll.epoll区别于联系