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);
  }

}

posted on 2016-06-23 11:04  奋斗青年一族  阅读(264)  评论(0编辑  收藏  举报