手机振动的设置
<!-- 设置手机震动权限 -->
<uses-permission android:name="android.permission.VIBRATE" />
//注意模拟器是模拟不了震动的,得真机测试 public class MainActivity extends Activity implements OnCheckedChangeListener { private Vibrator vibrator = null;// 振动器 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_content2); Switch sw1 = (Switch) findViewById(R.id.switch1);// 根据间隔的时间震动 Switch sw2 = (Switch) findViewById(R.id.switch2);// 指定震动的时间震动(一直震动) sw1.setOnCheckedChangeListener(this); sw2.setOnCheckedChangeListener(this); vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { switch (buttonView.getId()) { case R.id.switch1: if (isChecked) { // 根据指定的模式进行震动 // 第一个参数:该数组中第一个元素是等待多长的时间才启动震动, // 之后将会是开启和关闭震动的持续时间,单位为毫秒 // 第二个参数:重复震动时在pattern中的索引,如果设置为-1则表示不重复震动 // OFF(duration)/ON(duration)/OFF/ON... vibrator.vibrate(new long[] { 1000, 50, 50, 100, 50 }, -1); } else { vibrator.cancel();// 关闭震动 } break; case R.id.switch2: if (isChecked) { vibrator.vibrate(3500);// 启动震动,并持续指定的时间 } else { vibrator.cancel();// 关闭启动 } break; } } }