I have an activity that vibrates the phone for 9 seconds after it starts. I want the vibration action to be canceled when the activity leaves the foreground. Here is my current code:
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();
}
}