import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.app.Service;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.Toast;
public class VibrateTest extends Activity {
Vibrator vibrator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vibrate_test);
//获取系统的Vibrator服务
vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
}
//重写onTouchEvent方法,当用户触碰触摸屏时触发该方法
@Override
public boolean onTouchEvent(MotionEvent event) {
Toast.makeText(this, "手机振动", 5000).show();
//控制手机振动2秒
vibrator.vibrate(2000);
return super.onTouchEvent(event);
}
}