---
title: "android震动器"
author: "hualikejava"
date: "2011-04-08T10:56:38Z"
tags: []
categories: ["移动开发"]
slug: "android"
draft: false
lastmod: "2011-04-08T10:56:38Z"
id: 992061
url: "https://hualikejava.iteye.com/blog/992061"
---

vibrator是android提供的震动类，获得vibrator方法很简单直接getSystemService(Context.VIBRATOR_SERVICE)
 然后直接调用vibrate(long pattern, int repeat)这个方法，第一个参数long pattern是一个节奏数组，比如{1, 200}，
 而第二个参数是重复次数，-1为不重复，而数字直接表示的是具体的数字，和一般-1表示无限不同.
 
 触摸事件，当手触摸屏幕时震动。
 
```javaA

 @Override
	public boolean onTouchEvent(MotionEvent event) {
		if(event.getAction()==MotionEvent.ACTION_DOWN){
			vibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
			long[] pattern = {800, 50, 400, 30}; // OFF/ON/OFF/ON...
			vibrator.vibrate(pattern, 2);
			//vibrator.vibrate(900);
		}
		return super.onTouchEvent(event);
		
	}
	
```