android-Vibrator的使用
来源:互联网 发布:mina 数据接收不全 编辑:程序博客网 时间:2024/05/12 19:47
android手机中的震动由Vibrator实现。设置震动事件,需要知道其震动的时间长短、震动的周期等。
在android中,震动的时间一毫秒计算(1/1000秒),所以如果设置的时间值太小,会感觉不出来。
通过调用Vibrator的vibrate(long[] pattern, int repeat)方法实现。
前一个参数为设置震动的效果的数组,第二个参数为 -1表示只震动一次,为0则震动会一直持续。
一个demo:
- package com.shao.vibrator;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Vibrator;
- import android.widget.CompoundButton;
- import android.widget.Toast;
- import android.widget.CompoundButton.OnCheckedChangeListener;
- import android.widget.ToggleButton;
- public class VibratorActivity extends Activity {
- /** Called when the activity is first created. */
- private Vibrator vibrator;
- private ToggleButton tog1;
- private ToggleButton tog2;
- private ToggleButton tog3;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- init();
- tog1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- // TODO Auto-generated method stub
- if(isChecked){
- //设置震动周期
- vibrator.vibrate(new long[]{1000,10,100,1000}, -1);
- showToast("OK");
- }else{
- //取消震动
- vibrator.cancel();
- showToast("CANCEL");
- }
- }
- });
- tog2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- // TODO Auto-generated method stub
- if(isChecked){
- //设置震动周期
- vibrator.vibrate(new long[]{100,100,100,1000}, 0);
- showToast("OK");
- }else{
- //取消震动
- vibrator.cancel();
- showToast("CANCEL");
- }
- }
- });
- tog3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- // TODO Auto-generated method stub
- if(isChecked){
- //设置震动周期
- vibrator.vibrate(new long[]{1000,50,1000,50,1000}, 0);
- showToast("OK");
- }else{
- //取消震动
- vibrator.cancel();
- showToast("CANCEL");
- }
- }
- });
- }
- private void init(){
- tog1= (ToggleButton) findViewById(R.id.tog1);
- tog2= (ToggleButton) findViewById(R.id.tog2);
- tog3= (ToggleButton) findViewById(R.id.tog3);
- vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
- }
- private void showToast(String msg){
- Toast.makeText(this, msg, 1).show();
- }
- }
xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <RelativeLayout
- android:layout_marginTop="20dp"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="短震动"
- />
- <ToggleButton
- android:id="@+id/tog1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textOn="关闭"
- android:textOff="打开"
- android:layout_alignParentRight="true"
- />
- </RelativeLayout>
- <RelativeLayout
- android:layout_marginTop="20dp"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="长震动"
- />
- <ToggleButton
- android:id="@+id/tog2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textOn="关闭"
- android:textOff="打开"
- android:layout_alignParentRight="true"
- />
- </RelativeLayout>
- <RelativeLayout
- android:layout_marginTop="20dp"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="节奏震动"
- />
- <ToggleButton
- android:id="@+id/tog3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textOn="关闭"
- android:textOff="打开"
- android:layout_alignParentRight="true"
- />
- </RelativeLayout>
- </LinearLayout>
最后别忘了加上 <uses-permission android:name="android.permission.VIBRATE"/>权限
0 0
- Android Vibrator的使用
- Android Vibrator的使用
- Android Vibrator的使用
- android-Vibrator的使用
- android-Vibrator的使用
- Android Vibrator的使用
- Android Vibrator的使用
- Android Vibrator的使用
- Android Vibrator的使用
- Android Vibrator的使用
- 【Android】Vibrator的使用
- Android 振动器 Vibrator的使用
- android——Vibrator的使用
- Android菜鸟笔记-振动器Vibrator的使用
- Android中振动器(Vibrator)的使用
- android开发--振动器类Vibrator的使用
- Android Vibrator(震动器)的使用
- android系列:手机振动器(Vibrator)的使用
- 2014美国大学生数学建模竞赛C题,如何用excel提取合著者的关系
- xz压缩文件方法或命令
- ecshop数据库ecs_stats总是很大的解决方法
- 机房收费系统验收报告
- github 使用
- android-Vibrator的使用
- poj 3694 Network
- MFC入门学习(1)了解MFC
- SQL语句查询时防止SQL语句注入的方法之一
- 森纵培训第二十二天课程总结
- MFC入门学习之控件(2)创建一个对话框窗口并在运行时显示
- Houdini如何使用VEX创建材质?
- Android下的文件、数据、数据库
- Rational Rose无法启动,提示缺少suite objects.dll的问题