控制设备振动——Vibrator

作者:追风剑情 发布于:2016-8-5 15:31 分类:Android

需要添加权限

<uses-permission android:name="android.permission.VIBRATE" />

  1. public void setVibrator(){
  2. String vibratorService = Context.VIBRATOR_SERVICE;
  3. Vibrator vibrator = (Vibrator)getSystemService(vibratorService);
  4. //定义振动模式: {暂停时间,振动时间,暂停时间,振动时间,...}
  5. long[] pattern = {1000, 2000, 3000, 4000, 5000, 6000};
  6. vibrator.vibrate(pattern, -1);//使用振动方式(-1: 代表不重复)
  7. //vibrator.vibrate(1000);//振动1秒钟
  8. //取消振动(退出程序会自动停止任何已经初始化的振动)
  9. //vibrator.cancel();
  10. }

 

标签: Android

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号