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

原创粉丝点击