振动Vibrator
来源:互联网 发布:视觉对位算法角度 编辑:程序博客网 时间:2024/06/11 21:09
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="短振动" />
<ToggleButton
android:id="@+id/shorts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="关闭"
android:textOff="打开" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="长振动" />
<ToggleButton
android:id="@+id/longs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="关闭"
android:textOff="打开" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="节奏振动" />
<ToggleButton
android:id="@+id/rhythms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="关闭"
android:textOff="打开" />
</LinearLayout>
</LinearLayout>
java文件:
package com.example.vibratortest;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;
public class VibratorTest extends Activity implements ToggleButton.OnCheckedChangeListener{
private Vibrator vibrator = null;
private ToggleButton shorts = null, longs = null, rhythms = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vibrator_test);
init();
shorts.setOnCheckedChangeListener(this);
longs.setOnCheckedChangeListener(this);
rhythms.setOnCheckedChangeListener(this);
}
private void init() {
vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
shorts = (ToggleButton) findViewById(R.id.shorts);
longs = (ToggleButton) findViewById(R.id.longs);
rhythms = (ToggleButton) findViewById(R.id.rhythms);
}
private void showToast(String msg) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.vibrator_test, menu);
return true;
}
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.shorts:
if(arg1) {
//设置震动周期不循环
vibrator.vibrate(new long[]{1000, 1000, 2000, 10000}, -1);
//参数;延迟1000,震动时间为10,延迟时间100,震动时间1000,不循环
showToast("ok1");
} else {
//取消震动
vibrator.cancel();
showToast("cancel1");
}
break;
case R.id.longs:
if(arg1) {
//设置震动周期循环
vibrator.vibrate(new long[]{100, 100, 100, 1000}, 0);
showToast("ok2");
} else {
//取消震动
vibrator.cancel();
showToast("cancel2");
}
break;
case R.id.rhythms:
if(arg1) {
//设置震动周期断断续续
vibrator.vibrate(new long[]{2000, 1000, 2000, 1000, 2000}, 0);
//参数;延迟1000,震动时间50,延迟1000,延迟1000
showToast("ok3");
} else {
//取消震动
vibrator.cancel();
showToast("cancel3");
}
break;
default:
break;
}
}
}
权限:<uses-permission android:name="android.permission.VIBRATE"/>
- 振动Vibrator
- android振动功能Vibrator
- android Vibrator 振动使用
- android Vibrator 振动开发
- android Vibrator开启振动功能
- 使用Vibrator类写安卓振动详细步骤
- 使用Vibrator控制手机的振动
- 懒人爱家务_振动Vibrator
- mt2503三合一vibrator如何调整振动强度
- Android开发之控制手机振动(Vibrator的使用)
- Vibrator
- Vibrator
- Android的振动服务,vibrator第二个参数的真正含义
- android Vibrator
- android Vibrator
- Android & Vibrator
- Vibrator振动器
- Android vibrator
- hdu 1003-Max Sum
- 得用java agent instrument动态修改或替换相关
- dedecms爆后台文件技巧
- 程序员与英语
- 生产者-消费者问题
- 振动Vibrator
- qtextend4.4.3 在nuc900平台下编译问题及解决办法
- jQuery 异步上传插件 Uploadify 使用 (Java平台)
- JNI java.lang.UnsatisfiedLinkError 的一些常见错误
- 2014校招百度一面经验、二面经验
- Android开发训练一
- Bloom Filter 原理与应用
- iOS重绘机制drawRect
- BloomFilter--大规模数据处理利器