我有一项活动,它会在手机启动后振动 9 秒。我希望在活动离开前台时取消振动动作。这是我当前的代码:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// change image
screen.setImageResource(R.drawable.yama);
vibrateMe();
}
}, 9000);
}
public void vibrateMe() {
Vibrator vibrate = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
vibrate.vibrate(2000);
}
public void stopVibrating(Vibrator vibrate) {
vibrate.cancel();
}
@Override
public void onDestroy() {
super.onDestroy();
}
}