I am building an Android app and I need to have short vibrations at specific intervals in case some conditions are met.
The problem is that, when those conditions are met, even tho I set the device to vibrate for 2 seconds, it sometimes vibrates over 10 seconds and sometimes the vibrations don't stop until the device is restarted. Also, I noticed that the runnable inside the handler doesn't run at the correct interval of time. It should be every interval seconds, but it usually takes more than that.
Here's an example:
fullBatteryHandler.postDelayed(new Runnable() {
@Override
public void run() {
if(isBatteryFull){
Log.d("vibrate", "full battery");
v.vibrate(VibrationEffect.createOneShot(2000, VibrationEffect.DEFAULT_AMPLITUDE));
}
fullBatteryHandler.postDelayed(this, interval * 1000);
}
}, 1000);
Any ideas why that happens?
EDIT: I fixed the issue by adding a WakeLock.
Try this: