I have used the below code to vibrate the device.
public void vibrator() {
try {
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(5000);
} catch (Exception e) {
Log.d(TAG, "vibrator exception: " + e);
}
}
Can we programmatically detect this event (check is device vibrating)?
There's a bad way for rooted phones but you will get atleast something.
You can read the file at:
It stores the time left as microseconds when the device is vibrating. (tried on 5.1)
For unrooted phones, you can just check
"dumpsys vibrator"
Process output with a BufferedReader. It's updated once the vibrator finishes vibrating.Introduction
The Vibrator class does not have the
isVibrating()
method that you are looking for. It uses services, so you cannot easily override Vibrator and add in the extra functionality.ManagedVibrator
Below, is a ManagedVibrator class that is a wrapper for the Vibrator class. All Vibrator methods are included, with the additional
isVibrating()
method.The constant vibration methods with signatures that accept
long[] pattern
are easy to track becausecancel()
needs to be called to end the vibration. However, the one time vibration methods with signatures that acceptlong millseconds
are much harder to track.This implementation uses a
ScheduledThreadPoolExecutor
to track one time validation methods. It sets themIsVibrating
flag tofalse
just after a one time vibration method finishes.Usage
Limitations
No, you can't.
Same question is here.
You can check only if the device vibrating is supported:
See more: