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)?
No, you can't.
Same question is here.
You can check only if the device vibrating is supported:
Vibrator mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
boolean hasVibrator = mVibrator.hasVibrator();
See more:
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.
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 because cancel()
needs to be called to end the vibration. However, the one time vibration methods with signatures that accept long millseconds
are much harder to track.
This implementation uses a ScheduledThreadPoolExecutor
to track one time validation methods. It sets the mIsVibrating
flag to false
just after a one time vibration method finishes.
public class ManagedVibrator {
public static final String TAG = ManagedVibrator.class.getSimpleName();
private Context mContext;
private Vibrator mVibrator;
private boolean mIsVibrating = false;
private ScheduledThreadPoolExecutor mExecutor;
private Runnable mVibrationEndRunnable = new Runnable() {
@Override
public void run() {
setVibrating(false);
}
};
public ManagedVibrator(Context context) {
this.mContext = context;
mVibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
mExecutor = new ScheduledThreadPoolExecutor(1);
}
public boolean hasVibrator() {
return mVibrator.hasVibrator();
}
public void vibrate(long milliseconds) {
setVibrating(true);
mVibrator.vibrate(milliseconds);
notifyOnVibrationEnd(milliseconds);
}
// Requires API v21
public void vibrate(long milliseconds, AudioAttributes attributes) {
setVibrating(true);
mVibrator.vibrate(milliseconds, attributes);
notifyOnVibrationEnd(milliseconds);
}
public void vibrate(long[] pattern, int repeat) {
setVibrating(true);
mVibrator.vibrate(pattern, repeat);
}
// Requires API v21
public void vibrate(long[] pattern, int repeat, AudioAttributes attributes) {
setVibrating(true);
mVibrator.vibrate(pattern, repeat, attributes);
}
public void cancel() {
mVibrator.cancel();
setVibrating(false);
}
public boolean isVibrating() {
return mIsVibrating;
}
private void setVibrating(boolean isVibrating) {
mIsVibrating = isVibrating;
}
private void notifyOnVibrationEnd(long milliseconds) {
try {
mExecutor.schedule(mVibrationEndRunnable, milliseconds, TimeUnit.MILLISECONDS);
} catch (RejectedExecutionException e) {
Log.e(TAG, e.getMessage());
}
}
}
ManagedVibrator vibrator = new ManagedVibrator(this);
vibrator.vibrate(5000);
...
if (vibrator.isVibrating()) {
// Do something
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With